Mysql8.0分组后查出每组的指定数据
发布时间:2023-02-15 06:36:11 阅读:492

需求查出每个人的最高分数的那条数据

id name score
1 aa 14
2 aa 13

有三种思路

一:any_value函数(不符合要求)

select any_value(id),name,any_value(score) from table

 group by name

无法取出分数最高的那条数据

二:max聚合函数(不符合要求)

 select max(id),name,max(score) from table

 group by name

查出来的数据应该是

id name score
2 aa 14

这已经不是原有的数据了

三:窗口函数

这么说来 窗口函数更适合控制自己最想要的数据

select o.* from (select i.*, rank() over (partition by name order by score) as ranking from

(select id,name,score from table) i) o where ranking = 1
发表评论
使用 Nuxt 3 构建 | 部署于 Kubernetes | 托管于 狗云
Copyright © 2020-2024 | 网站已续航 1530 天