我有一个与以下表格类似的表格。我希望获得不同状态的单独计数

时间:2018-07-25 17:04:00

标签: mysql sql

表格数据

id |name     | status         |location 
----------------------------------
 1 |ABCD     |completed       | Delhi
 2 |QWER     |construction    | Mumbai
 3 |RBVF     |To_launch       | Chennai
 4 |POIU     |ready           | Kolkata
 5 |WABCD    |completed       | Delhi
 6 |RQWER    |construction    | Mumbai
 7 |TRBVF    |To_launch       | Chennai
 8 |UPOIU     |ready           | Kolkata
 9 |RBVF      |To_launch       | Chennai
 10|POIU      |ready           | Kolkata
 11|WABCD     |completed       | Delhi

让我说我的输出应该像

construction status      count
completed                  4
To_launch                  3
ready                      3
construction               2

我尝试过类似的事情

select d.status, count(d.status)
from data d
where d.status = 'completed'

但是我希望所有状态结果都在一个查询中。但是我无法弄清楚。TIA..请提出一些改进SQL的建议。

1 个答案:

答案 0 :(得分:0)

使用聚合函数countgroup by子句

   select status as `construction status` ,count(*)as Cnt from Yourtable
    group by status

下面的查询还会在您的评论中显示所需的总数

   select status as `construction status`, count(*) as count, total from  
   Yourtable,(select count(*) as total from Yourtable) t2 
   group by status

http://www.sqlfiddle.com/#!9/8abcce/4