gpt4 book ai didi

mysql - 从 mysql 中的数据库中获取 max(top some things data)

转载 作者:行者123 更新时间:2023-11-29 06:57:56 25 4
gpt4 key购买 nike

伙计们,我想从下表中获取特定年份的前 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

演示:http://www.sqlfiddle.com/#!2/89c06/6

关于mysql - 从 mysql 中的数据库中获取 max(top some things data),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11618460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com