mysql - 多行COUNT查询

时间:2016-07-20 16:34:15

标签: mysql sql select count

例如,我有一个数据

ID    Doctor    Status
1     Wooka     Approved
2     Jamba     Approved
3     Jamba     Approved
4     Wooka     Approved
5     Wooka     Approved
6     Wooka     Approved
7     Wooka     Approved
.    .          .
.    .          .
.    .          .
99    

对于前7行(演示目的,不限于7)我希望它像这样输出

Doctor    Count
Wooka     5
Jamba     2

虽然我不确定如何在COUNT()

中这样做
SELECT COUNT(*) FROM table WHERE status='Approved'

我真的不知道如何灵活应对我的要求。

2 个答案:

答案 0 :(得分:2)

SELECT
  t.doctor,
  COUNT(*) AS c
FROM
  table t
WHERE
  t.status = 'Approved'
GROUP BY
  t.doctor

答案 1 :(得分:0)

您可以在子查询中使用limit子句来限制返回的行数,然后使用执行count的聚合查询包装此查询:

SELECT   doctor, COUNT(*)
FROM     (SELECT   doctor
          FROM     mytable
          WHERE    status = 'Approved'
          ORDER BY id ASC
          LIMIT    7 -- or some other number
         ) t
GROUP BY doctor