mysql - 我有一个类似于以下的表格。我想要不同状态的单独计数

标签 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 能力。

最佳答案

使用聚合函数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

关于mysql - 我有一个类似于以下的表格。我想要不同状态的单独计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51524271/

相关文章:

php - MySQLi速度: multiple queries or one

php - 具有相同属性的多种形式

c# - Oracle mysqld.data.dll 和 MariaDB mysql.data.dll for c# 有什么区别?

sql - 如何在 SQL 中使用变量引发错误

MySQL 按月、年计数,如果没有则显示 0

php - Laravel 下拉列表

c# - 带有数据透视表的 CTE

mysql - 商品属性表交集查询

java - 在 JDBC 中将时间戳存储为 Long .. 好还是坏?

mysql - 如何使用本地 phpMyAdmin 客户端访问远程服务器?