mysql - 我有一个类似于以下的表格。我想要不同状态的单独计数可用
问题描述
表数据
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 中改进自己。
解决方案
使用聚合函数count
和group 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
推荐阅读
- csv - Jinja 通过 Ansible 剧本到 CSV
- sql - 在 postgresql 中将数据从一个表移动到另一个表
- amazon-cognito - AWS Cognito 身份验证令牌 - 快速
- python - 提高返回列表中元素第一次出现的索引的函数的时间复杂度
- ios - Swift / iOS Core Data - Save Large Amount of Records in Background Thread
- php - Laravel 上的表格分页问题
- javascript - 为什么要添加多个类?
- r - 基于特征向量的子集数据
- scala - 在 Scala 程序的 shell 命令中使用变量(不是 REPL)
- python - Python code to keep only a set of html tags in a input string