gpt4 book ai didi

mysql - 不使用 join 将两个表合二为一

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

有两个不同的表,我希望它们合并为一个:

表 1:

+-------------+------------+  
| ProdCode | QTY |
+-------------+------------+
| 1 | 1 |
+-------------+------------+
| 2 | 4 |
+-------------+------------+
| 3 | 4 |
+-------------+------------+
| 4 | 5 |
+-------------+------------+

表 2:

+-------------+------------+  
| MatCode | QTY |
+-------------+------------+
| ZZ | 21 |
+-------------+------------+
| AA | 44 |
+-------------+------------+
| RR | 44 |
+-------------+------------+
| QQ | 52 |
+-------------+------------+

最终结果:

+-------------+------------+  
| Code | QTY |
+-------------+------------+
| 1 | 1 |
+-------------+------------+
| 2 | 4 |
+-------------+------------+
| 3 | 4 |
+-------------+------------+
| 4 | 5 |
+-------------+------------+
| ZZ | 21 |
+-------------+------------+
| AA | 44 |
+-------------+------------+
| RR | 44 |
+-------------+------------+
| QQ | 52 |
+-------------+------------+

如果 ProdCode 和 MatCode 不相关,是否可以这样做?

最佳答案

SELECT ProdCode, QTY FROM [table1]
UNION ALL
SELECT MatCode, QTY FROM [table2]

这将从两个表中选择记录并将它们作为一个结果集返回。

关于mysql - 不使用 join 将两个表合二为一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27422498/

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