MySQL小组摘要

时间:2010-03-06 21:37:39

标签: mysql group-by

在我的会员表中,我希望在“位置”1-7中汇总所有不同的人员,其中包括有多少人在线和离线。

SELECT location, COUNT(*) FROM members GROUP BY location;

返回:

1 10  
2 5  
3 4  
4 12  
5 6  
6 3  
7 19  

对于状态为0(离线)且状态为1(在线)的成员,我想要COUNT。我该怎么做?

2 个答案:

答案 0 :(得分:4)

SELECT location, status, COUNT(*) FROM members GROUP BY location, status

作为一行:

SELECT
    location,
    COUNT(status) - SUM(status) as offline,
    SUM(status) as online
FROM members
GROUP BY location

答案 1 :(得分:2)

选择位置,状态,COUNT(*)FROM成员GROUP BY位置,状态