Wednesday, May 29, 2019

mysql - Writing a single query for mutliple queries with same where condition



select column1,column2,column3 from table1
where column5=0 and column6=0;



select column4 from table2

where column5=0 and column6=0;



These are two sql statements reading data from table1 & table2, is there a way instead of 2 single queries, can i write in a single query?



Thanks.


Answer



You could use UNION to combine the results:



SELECT
column1,

column2,
column3,
NULL AS column4
FROM table1
WHERE column5 = 0
AND column6 = 0

UNION

SELECT

NULL AS column1,
NULL AS column2,
NULL AS column3,
column4
FROM table2
WHERE column5 = 0
AND column6 = 0

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...