我目前正在使用SQL创建两个表,但是我需要联合它们,并且当涉及到分组时不清楚如何执行此操作
Select ...
group by email, column 1
这会创建两个这样的表:
Table1
+--------------+----------+
| email | Column1 |
+--------------+----------+
| 1 | x |
+--------------+----------+
| 2 | x |
+--------------+----------+
table2
+--------------+----------+
| email | Column1 |
+--------------+----------+
| 1 | x |
+--------------+----------+
| 2 | x |
+--------------+----------+
我想使用union将它们合并到此
中TableTotal
+--------------+----------+----------+----------+
| email | Column1 | Column2 | Column3 |
+--------------+----------+----------+----------+
| 1 | x | x | x |
+--------------+----------+----------+----------+
| 2 | x | x | x |
+--------------+----------+----------+----------+
| 1 | x | x | x |
+--------------+----------+----------+----------+
| 2 | x | x | x |
+--------------+----------+----------+----------+
但是当我也对选择进行分组时,我不知道如何使用联合。当我尝试这样做时,我收到错误“column1 is ambiguous”。
答案 0 :(得分:1)
您需要UNION ALL或UNION(删除重复项)。
SELECT ... GROUP BY ...
UNION ALL
SELECT ... GROUP BY ...;