MySQL - 组中的一组

时间:2015-05-11 07:18:32

标签: mysql sql

我有一张这样的表:

doctor_name   |   medicine

John                     a
John                     b
John                     a
John                     b
Kevin                    c
Kevin                    c
Kevin                    c
Kevin                    a

我希望按doctor_namemedicine进行分组。我的愿望输出是这样SELECT

doctor_name   |   medicine   |   COUNT(medicine)
John                     a             2
John                     b             2
Kevin                    a             1
Kevin                    c             3

我不知道如何在MySQL中使用普通的GROUP BY来做到这一点。真的很感激,如果有人可以帮我这个。

3 个答案:

答案 0 :(得分:0)

您需要按doctor_namemedicine

进行分组
select
doctor_name,
medicine,
count(*) as  total
from table_name
group by doctor_name,medicine

答案 1 :(得分:0)

SELECT
doctor_name,
medicine,
count(*) as  total_medicine
FROM table_name
GROUP BY doctor_name,medicine;

答案 2 :(得分:0)

尝试使用此查询

select doctor_name,medicine,count(*) as count
from my_table group by doctor_name,medicine