所以我有这个mysql语句,我只想获取id的值一次。这是我尝试过但它仍然多次写入ID。
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
GROUP BY id
举例:
1 - swe
1 - swe
2 - uk
2 - uk
etc
我想:
1 - swe
2 - uk
有人建议吗?非常适合任何帮助!感谢
答案 0 :(得分:1)
离开小组:
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
正如W3C所指出的,UNION默认只显示了不同的值: http://www.w3schools.com/sql/sql_union.asp
答案 1 :(得分:1)
您无需添加group by子句因为UNION已经区分了您的2个结果集。
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
<强> SQL Fiddle Demo 强>
答案 2 :(得分:0)
SELECT t1.id,t3.location FROM table1 t1 内联接table3 t3 on(t1.id = t3.id)
UNION
SELECT t2.id,t3.location FROM table2 t2 内联接table3 t3 on(t2.id = t3.id)
分组1;