伙计们,我想从下表中获取特定年份的前 3 大疾病及其计数。我应该运行什么查询?
mysql> select id,dname,d_id,entrydate from patient_master;
+----+------------------+------+------------+
| id | dname | d_id | entrydate |
+----+------------------+------+------------+
| 1 | Root Canal | 1 | 2012-08-02 |
| 2 | Cosmetic Filling | 3 | 2012-05-10 |
| 3 | Root Canal | 1 | 2012-05-25 |
| 4 | High BP | 6 | 2012-07-09 |
| 5 | Root Canal | 1 | 2012-07-10 |
| 6 | Normal Filling | 2 | 2012-05-10 |
| 7 | Maleria | 4 | 2012-07-10 |
| 8 | Maleria | 4 | 2012-07-12 |
| 9 | Typhoid | 5 | 2012-07-12 |
+----+------------------+------+------------+
9 rows in set (0.00 sec)
最佳答案
使用 group by
子句按疾病组合结果,并使用 count(*)
计算每种疾病的记录数。然后您可以从最大到最少排序并使用 limit 3
只获得前 3 个。我还包含一个 where
子句以仅过滤 2012 中的记录
。
select count(*), dname
from patient_master
where entrydate between '2012-01-01' and '2013-01-01'
group by dname
order by count(*) desc
limit 3
关于mysql - 从 mysql 中的数据库中获取 max(top some things data),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11618460/