GROUP BY并以相同的值显示一次

时间:2018-09-13 08:15:54

标签: mysql sql

我想在多个具有相同值的行中显示一次

我的查询是这样

SELECT name, 
       GROUP_CONCAT(class) class, 
       COUNT(*) AS total 
FROM report 
GROUP BY name

以及类似的数据

+----+----------+-------+
| ID | NAME     | CLASS | 
+----+----------+-------+
|  1 | John     |  10   | 
|  2 | James    |  21   | 
|  3 | James    |  21   | 
|  4 | John     |  10   | 
|  5 | Augie    |  22   | 
|  6 | Augie    |  22   | 
|  7 | John     |  10   | 
+----+----------+-------+

我想这样输出,名称将显示一次

Name : John, class : 10, Total : 3
Name : James, class : 21, Total : 2
Name : Augie, class : 22, Total : 2

2 个答案:

答案 0 :(得分:1)

尝试此查询

SELECT name,Class,COUNT(*) AS total FROM report GROUP BY name,Class

答案 1 :(得分:0)

使用汇总和分组依据:

select name, class, count(id) as total
from tablename
group by name, class