我希望在mysql中使用来自单个表的单个查询来获得以下输出。
Total Pending Critical Completed
120 45 45 30
现在我正在为不同的列使用4个查询像
select count(*) total from Y; -- for first column
select count(*) as Pending from Y where status = 0--- for second column
select count(*) as critical from Y where type = 'Critical' -- for third column
我想要什么,我想在一个查询中类似的东西
select count(*) as total,count(*) pending where XYZ,
我不知道在哪里写什么,因为每个输出的条件不同,如果我们写
select count(*) as total,count(*) pending where status = 0
然后它给出
total pending
45 45
但是我想要
total pending
120 45
请帮帮我,
您可以尝试使用CASE WHEN THEN,如下所示:
select SUM(CASE WHEN status = 0 THEN 1 END) as 'Pending ',
SUM(CASE WHEN type = 'Critical' THEN 1 END) as 'critical',
COUNT(*) as 'Total'
from Y
我能看到的最好的方法是像这样的查询
SELECT ( select count() total from Y) as total ,
( select count() total from Y WHERE status = 0) as pending