gpt4 book ai didi

mysql - 在第三个表中合并 2 个 mysql 表

转载 作者:行者123 更新时间:2023-12-01 00:18:24 24 4
gpt4 key购买 nike

我在mySql中有3个表==> tableA, tableB, tableC

在表A中,我有以下内容

+---------+----+----+--------+------------+--+
| Name | N1 | N2 | Color | State | |
+---------+----+----+--------+------------+--+
| John | 60 | 50 | Red | Newyork | |
| Tom | 70 | 60 | Green | Kansas | |
| Mathew | 50 | 40 | Blue | Texas | |
| James | 40 | 30 | Yellow | Texas | |
| SSS | 70 | 60 | Pink | Washington | |
+---------+----+----+--------+------------+--+

在表B中,我有以下内容

+--------+----+----+
| S.Name | N3 | N4 |
+--------+----+----+
| Carl | 10 | 18 |
| Jason | 15 | 9 |
| Cindy | 13 | 12 |
| Tim | 7 | 18 |
| Pam | 15 | 14 |
+--------+----+----+

在表C中,我想要以下内容

+---------+----+----+--------+------------+--------+
| Name | N1 | N2 | Color | State | S.Name |
+---------+----+----+--------+------------+--------+
| John | 60 | 50 | Red | Newyork | |
| Tom | 70 | 60 | Green | Kansas | |
| Mathew | 50 | 40 | Blue | Texas | |
| James | 40 | 30 | Yellow | Texas | |
| SSS | 70 | 60 | Pink | Washington | |
| | 10 | 18 | | | Carl |
| | 15 | 9 | | | Jason |
| | 13 | 12 | | | Cindy |
| | 7 | 18 | | | Tim |
| | 15 | 14 | | | Pam |
+---------+----+----+--------+------------+--------+

请帮我做这个sql查询。谢谢

最佳答案

作为选择结果,您可以使用简单的union all:

select Name as Name, N1 as N1, N2 as N2, Color as Color, State as State, null as S_Name from tableA
union all
select null as Name, N3 as N1, N4 as N2, null as Color, null as State, S_Name as S_Name from tableB

关于mysql - 在第三个表中合并 2 个 mysql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43797971/

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