gpt4 book ai didi

mysql - 在 mariaDB 中完全连接 2 个表

转载 作者:太空宇宙 更新时间:2023-11-03 11:22:14 24 4
gpt4 key购买 nike

我有两张 table

Table1
Year,Month, data
2017,1,2
2018,2,10

Table2
Year,Month,data2
2017,1,5
2019,2,2

我正在尝试将这些表合并为 1 个表,我们从两个表中获取所有行,如下所示。

Year,Month,data,data2
2017 ,1,2,5
2018,2,10,NULL
2019,2,NULL,2

似乎标准外部连接在这里不起作用,我也不能使用 Union ALL是否有某种外部联接查询可以完成此操作?

最佳答案

您应该使用 UNION 从两个表中获取所有年份和月份,并使用左连接将其与表 1 和表 2 相关联

select a.Year , a.Month, b.data, c.data2
from (
select Year,Month
from Table1
union
select Year,Month
from Table2
) a
left join table1 b on a.Year = b.Year and a.month = b. month
left join table2 c on a.Year = c.Year and a.month = c. month

关于mysql - 在 mariaDB 中完全连接 2 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684018/

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