-1

Good day.

Database: MySQL

Table:

name (varchar 200)
status (enum 'open','close','pause')

For get count all name with all status me need use 4 query:

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

Anyone know how get all counts in one sql query and really it?

4

4 回答 4

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
于 2013-10-11T09:57:52.010 回答
2

使用以下查询:

SELECT status, count(name) as cnt FROM TABLE GROUP BY status;
于 2013-10-11T09:58:20.557 回答
1

你可以试试

Select Count(Status),Status,(Select Count(Status) as TotalCount From TABLE) From TABLE Group By Status;
于 2013-10-11T10:53:33.057 回答
-1

另一种选择:-

SELECT status, COUNT(*) as aCount,
FROM TABLE
GROUP BY status
UNION ALL 
SELECT 'All', COUNT(*) as aCount,
FROM TABLE
于 2013-10-11T10:13:02.737 回答