views:

23

answers:

3

How do you get the UNION of these two queries:

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

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

dom and sub are integers, both queries return a set of integers, how do you then get the union of these two sets??

Any assistance appreciated...

+3  A: 
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

...

Arnaud F.
+5  A: 

You have got an answer using UNION, but why not just

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

or

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

(assuming you are indeed talking about the same table in both queries)?

Thilo
+1  A: 

Following query will remove similar records i.e. will show only distinct

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

if you want all records ,use

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
gajendra.bang