gpt4 book ai didi

mysql - 如何在mysql中进行全外连接合并两个表?

转载 作者:行者123 更新时间:2023-11-29 05:36:28 26 4
gpt4 key购买 nike

我有两个表payroll_advancepayroll_advrtn,我应该做完全外部连接以获得我需要的结果。但是,我确定完全外部连接不是'在 mysql 中不可能,而且我知道使用联合可以实现完全外部连接。但我不知道如何在以下查询中加入。

我的 payroll_advance 表产生以下结果。

SELECT _id,_tid,_dt,sum(_amount) as _advance FROM payroll_advance WHERE YEAR( _dt )=YEAR(CURDATE()) AND MONTH(_dt) = MONTH(CURDATE()) group by _tid;

+-----+-------+------------+---------+
| _id | _tid | _dt | _advance|
+-----+-------+------------+---------+
| 17 | hjg | 2012-04-18 | 2151 |
| 22 | RKT01 | 2012-04-10 | 2098 |
| 14 | RKT04 | 2012-04-18 | 1511 |
| 16 | RKT09 | 2012-04-09 | 250 |
| 15 | RKT10 | 2012-04-17 | 1313 |
| 8 | RKT21 | 2012-04-03 | 1321 |
| 19 | RKT31 | 2012-04-26 | 2512 |
| 20 | RKT33 | 2012-04-10 | 2250 |
| 25 | T01 | 2012-04-11 | 2500 |
+-----+-------+------------+---------+

payroll_advrtn 给出以下结果。

SELECT _id,_tid,_dt,sum(_amount) as _advrtn FROM payroll_advrtn WHERE YEAR( _dt ) = YEAR(CURDATE()) AND MONTH(_dt) = MONTH(CURDATE()) group by _tid;
+-----+-------+------------+---------+
| _id | _tid | _dt | _advrtn |
+-----+-------+------------+---------+
| 9 | RKT02 | 2012-04-10 | 2500 |
| 8 | RKT04 | 2012-04-20 | 150 |
+-----+-------+------------+---------+

但我想要通过组合以上两个结果得到如下结果。

 +------+-------+-------+------------+----------+---------+
| _id | _tid | _tid | _dt | _advance | _advrtn |
+------+-------+-------+------------+----------+---------+
| 17 | hjg | NULL | 2012-04-18 | 2151 | NULL |
| 22 | RKT01 | NULL | 2012-04-10 | 999 | NULL |
| 14 | RKT04 | RKT04 | 2012-04-18 | 11 | 150 |
| 16 | RKT09 | NULL | 2012-04-09 | 250 | NULL |
| 15 | RKT10 | NULL | 2012-04-17 | 1313 | NULL |
| 8 | RKT21 | NULL | 2012-04-03 | 1321 | NULL |
| 19 | RKT31 | NULL | 2012-04-26 | 2512 | NULL |
| 20 | RKT33 | NULL | 2012-04-10 | 2250 | NULL |
| 25 | T01 | NULL | 2012-04-11 | 2500 | NULL |
| NULL | NULL | RKT02 | NULL | NULL | 2500 |
+------+-------+-------+------------+----------+---------+

任何帮助将不胜感激。谢谢!!

最佳答案

为了执行FULL OUTER JOIN you can do LEFT OUTER JOINUNIONRIGHT OUTER JOIN( 前提是 MySql还不支持 FULL OUTER JOIN ):

select * from A as a
left outer join B as b on a.col = b.col
union
select * from A as a
right outer join B as b on a.col = b.col

请注意,您可以对 AB 使用子查询 - 这应该适用于您的查询。在你的情况下:

select * from (SELECT * FROM t1) as a
left outer join (SELECT * FROM t2) as b on a._tid = b._tid
union
select * from (SELECT * FROM t1) as a
right outer join (SELECT * FROM t2) as b on a._tid = b._tid

结果等于(前提是我在复制粘贴您的数据时没有犯错):

+------+-------+------------+----------+------+-------+------------+----------+
| _id | _tid | _dt | _advance | _id | _tid | _dt | _advartn |
+------+-------+------------+----------+------+-------+------------+----------+
| 17 | hjg | 2012-04-18 | 2151 | NULL | NULL | NULL | NULL |
| 22 | RKT01 | 2012-04-10 | 2098 | NULL | NULL | NULL | NULL |
| 14 | RKT04 | 2012-04-18 | 1511 | 8 | RKT04 | 2012-04-20 | 150 |
| 16 | RKT09 | 2012-04-09 | 250 | NULL | NULL | NULL | NULL |
| 15 | RKT10 | 2012-04-17 | 1313 | NULL | NULL | NULL | NULL |
| 8 | RKT21 | 2012-04-03 | 1321 | NULL | NULL | NULL | NULL |
| 19 | RKT31 | 2012-04-26 | 2512 | NULL | NULL | NULL | NULL |
| 20 | RKT33 | 2012-04-10 | 2250 | NULL | NULL | NULL | NULL |
| 25 | T01 | 2012-04-11 | 2500 | NULL | NULL | NULL | NULL |
| NULL | NULL | NULL | NULL | 9 | RKT02 | 2012-04-10 | 2500 |
+------+-------+------------+----------+------+-------+------------+----------+

关于mysql - 如何在mysql中进行全外连接合并两个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com