MySQL - 组中组

标签 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 来做到这一点。如果有人能帮助我,我真的很感激。

最佳答案

您需要按 doctor_namemedicine 进行分组

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

关于MySQL - 组中组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30161718/

相关文章:

mysql - 跳过计算用户自己的评论

mysql - 如果它们相同,如何在mysql中连接两行数据

sql - 使用 Postgres 从每组中取出一个对象

java.math.BigInteger 无法转换为 java.math.BigDecimal

sql - 用于 FireBird 数据库的 Firebird 数据库和 Linux Red Hat Base Server

mysql - 如何在 sequelize 中按聚合函数排序

mysql - 从查询双方获取数据

python - 如果遇到异常则跳过 XML 读取循环的迭代 Python

mysql - 我可以让 MySQL 触发器在实际触发语句之前执行吗?

sql - MYSQL 的 select 方法有哪些?