MySQL How do you return the UNION of these two queries?
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 q开发者_运维技巧ueries return a set of integers, how do you then get the union of these two sets??
Any assistance appreciated...
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)?
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
...
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
精彩评论