2013-03-20 53 views
0
Results 1 
    Zac 
    Dave 
    Ned 

Results 2 
    Eric 
    Mark 
    Zac 

这是选择查询的输出。MySql Union仅返回BOTH中的值

select names from table where id=1 UNION select names from tables where id=2; 

我想从这两个结果中包含的结果中选择全部。联盟返回所有名称(Zac只有一次)。我如何获得查询只返回Zac?

+2

您正在寻找*路口*,而不是工会。看到这个问题:[在MySQL中相交](http://stackoverflow.com/questions/2621382/intersect-in-mysql) – 2013-03-20 17:18:49

回答

1

这应做到:

SELECT name FROM table1 
INNER JOIN table2 
USING (name) 

结果

| NAME | 
-------- 
| Zac |

See the demo

+0

不幸的是,MySQL没有INTERSECT运算符。 – 2013-03-20 17:25:39

+0

@DanJ哎呀;更新。 – Kermit 2013-03-20 17:26:34

0

只需使用两个表之间的INNER JOIN

SELECT a.name 
FROM table1 AS a 
JOIN table2 AS b ON a.name = b.name