我想使用来自单个表的单个查询来跟踪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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句