在不使用连接的情况下将两个表组合在一起

时间:2014-12-11 12:06:23

标签: mysql join

有两种不同的表格,我希望它们合二为一:

表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不相关,是否可以这样做?

1 个答案:

答案 0 :(得分:4)

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

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