开发者

Database Query to get sum of count from different tables in a single query

开发者 https://www.devze.com 2023-01-31 01:48 出处:网络
I am writing a query in Mysql database, in which Query 1 returns count() say result is 10 and Query 2 returns Count() say result is 30

I am writing a query in Mysql database, in which Query 1 returns count() say result is 10 and Query 2 returns Count() say result is 30

But I want to get the result as 40, which is sum of both

what are my options to get a single query giving me the result开发者_高级运维.


You should use UNION ALL to union also the same valued counts like 30+30.

select SUM(n) as total
from (
  (select count(*) as n from table1)
  UNION ALL
  (select count(*) as n from table2)
) t;


select sum(num) as total
from (
  (select count(*) as num from table1)
  UNION ALL
  (select count(*) as num from table2)
) a;
0

精彩评论

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