美好的一天。
数据库:MySQL
表格
name (varchar 200)
status (enum 'open','close','pause')
要获取所有name
的所有状态,我需要使用4个查询:
SELECT count(*) FROM TABLE //count all names
SELECT count(*) FROM TABLE where status = open //count names where status = open
SELECT count(*) FROM TABLE where status = close //count names where status = close
SELECT count(*) FROM TABLE where status = pause //count names where status = pause
任何人都知道如何在一个 sql查询中获得所有重要信息吗?
答案 0 :(得分:2)
SELECT count(*) as total_count,
sum(status = 'open') as status_open_count,
sum(status = 'close') as status_close_count,
sum(status = 'pause') as status_pause_count
FROM TABLE
答案 1 :(得分:2)
使用以下查询:
SELECT status, count(name) as cnt FROM TABLE GROUP BY status;
答案 2 :(得分:1)
你可以尝试
Select Count(Status),Status,(Select Count(Status) as TotalCount From TABLE) From TABLE Group By Status;
答案 3 :(得分:-1)
另一种选择: -
SELECT status, COUNT(*) as aCount,
FROM TABLE
GROUP BY status
UNION ALL
SELECT 'All', COUNT(*) as aCount,
FROM TABLE