开发者

Writing a single query for mutliple queries with same where condition

开发者 https://www.devze.com 2023-01-19 20:01 出处:网络
select column1,column2,column开发者_开发知识库3 from table1 where column5=0 and column6=0; select column4 from table2
select column1,column2,column开发者_开发知识库3 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消