MySQL如何创建UNION并检查两个选择的结果?

时间:2012-02-28 17:55:10

标签: mysql union

我想做的例子

(SELECT name result FROM stuff2,finding) h 
UNION 
(SELECT name result FROM stuff2 where h.name != stuff2.name)

如何检查union之间的值?

注意我在同一张桌子上打电话。

3 个答案:

答案 0 :(得分:1)

你做不到。你想要一个JOIN:

SELECT s1.*, s2.*
FROM stuff2 AS s1
JOIN stuff2 AS s2
  ON s1.name != s2.name

答案 1 :(得分:1)

假设联合的两个子查询从不同的表中获取数据:

( SELECT * FROM stuffOne ) s 
UNION 
( SELECT * 
  FROM stuffTwo s2
  WHERE s2.name NOT IN
        ( SELECT name FROM stuffOne )
)

答案 2 :(得分:0)

为什么不使用左连接,如下所示:

select 
     stuff2.name,
     ISNULL(finding.result, stuff2.result) as result
from stuff2
left join finding
on stuff2.ID = finding.ID