需要单个查询的多个输出 - mysql

时间:2015-11-13 06:56:43

标签: mysql

我想在单个表中使用单个查询在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

请帮助我,

2 个答案:

答案 0 :(得分:1)

你可以尝试使用CASE,然后这样:

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

答案 1 :(得分:0)

我可以看到你做的最佳方式是像

这样的查询
SELECT ( select count() total from Y) as total ,  
    ( select count() total from Y WHERE status = 0) as pending