2010-10-23 61 views
0

你如何获得这两个查询的UNION:MySQL如何返回这两个查询的UNION?

SELECT DOM,子FROM表WHERE table.dom = x或table.sub = X

SELECT DOM,子FROM表WHERE table.dom = Y OR table.sub = Y

dom和sub是整数,两个查询都返回一组整数,那么如何获得这两组的集合?

任何帮助赞赏...

回答

5

你使用UNION有了答案,但为什么不只是

SELECT dom,sub FROM table 
    WHERE table.dom = X OR table.sub = X OR table.dom = Y OR table.sub = Y 

SELECT dom,sub FROM table 
    WHERE table.dom in (X,Y) OR table.sub in (X,Y) 

(假设你是ind在两个查询中谈论同一张表)?

3
SELECT dom,sub FROM table WHERE table.dom = X OR table.sub = X 
UNION 
SELECT dom,sub FROM table WHERE table.dom = Y OR table.sub = Y 

...

1

下面的查询将删除类似记录,即只显示不同

SELECT dom,sub FROM table WHERE table.dom = X OR table.sub = X 
UNION 
SELECT dom,sub FROM table WHERE table.dom = Y OR table.sub = Y 

如果你希望所有的记录,使用

SELECT dom,sub FROM table WHERE table.dom = X OR table.sub = X 
UNION ALL 
SELECT dom,sub FROM table WHERE table.dom = Y OR table.sub = Y