Mysql 分组查询top n(多种方法)

Mysql 分组查询top n(多种方法),第1张

查询每门科目的前3名。

对于查询的结果,一般有两种情况

表所有数据为:

情况1:对于分数相同的人,其后面的人 紧跟着名次排,直到排够名次3,就不再往后取了。

情况2:对于分数相同的人,若当前相同名次的人数大于或等于 3, 则相同分数其后面的人不再参于top3了。

在情况2中 ,为什么”李四 - java“ 这行没有了呢?

可以这样理解,在情况2中相当于使用了 名次空缺 , 分数相同的人其后面那个人,的名次为 前面的人数+1 , 这里的 ”李四-java“这行,他的名次应当是 5 , 所有top3自然取不到 ”李四-java“这行。

情况2和情况 写法都是对应的, 需要注意的是 并列名次,后面的人 是否需要保持名次空缺。那么统计的时候就需要根据情况去重。

参考链接:

https://blog.csdn.net/weixin_44497013/article/details/107317719

https://leetcode-cn.com/problems/department-top-three-salaries/solution/mysqlzi-ding-yi-bian-liang-shi-xian-fen-lei-pai-xu/

不可以 。

在mysql 中用limit

比如要查询user表中的前10条数据

用 select * from user limit 10

还可以分页查询 比sqlserver 方便多拉。

分页用 select * from user limit 10,2 //10代表 显示前10条,2表示 显示第2也的数据

前两天同事有个 MySQL 数据分组的需求,如下测试数据,需要找出每个 name 分组中 create_date 最近的记录:

需要注意的是,此处用的 MySQL 是5.6,最初是使用这条语句:

<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10pxmargin-bottom: 10pxborder-radius: 5pxbox-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px">select name, value, create_date, update_date from t1 group by name order by create_date desc </pre>

用这条 SQL 得到的其实只是每个 name 分组中最先插入的记录,然后按照 create_date 进行了降序排列,和原始需求,完全不同。

此时可采用分而治之的策略,先做排序,再做分组:

<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10pxmargin-bottom: 10pxborder-radius: 5pxbox-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px">select * from (select name, value, create_date, update_date from t1 order by create_date desc) t group by t.name </pre>

当然,针对此需求,可能有其他方法,有兴趣的朋友,可以尝试写写,共享一下。

可能有细心的朋友会发现个问题,就是上述 SQL 中的 group by ,好像有些奇怪,如果按照常规,select 中的字段需要出现在 group by 中,上述语句竟然没报错?

如果我们在 MySQL 5.7 执行相同的语句:

<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10pxmargin-bottom: 10pxborder-radius: 5pxbox-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px">select name, value, create_date, update_date from t1 group by name order by create_date desc </pre>

因此从5.6升级到5.7,很可能出现这种相同的 SQL 执行结果不同的现象,这对兼容性测试的要求就会很高,究其原因,一方面是特性决定的,另一方面就是各种配置参数不同导致的。

可以在5.7的 sql_mode 中删除这个 ONLY_FULL_GROUP_BY ,即可达到5.6相同效果了,或者改写 SQL ,例如:

<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10pxmargin-bottom: 10pxborder-radius: 5pxbox-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px">select * from t1 a where create_date = (select max(create_date) from t1 b where a.name = b.name) </pre>

或者,

<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10pxmargin-bottom: 10pxborder-radius: 5pxbox-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px">select * from t1 a where not exists (select * from t1 b where a.name = b.name and b.create_date >a.create_date) </pre>

MySQL 8.0支持 row_number()函数, *** 作应该和如下 Oracle 相近的。

Oracle 中可以使用 row_number()实现此需求:

<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10pxmargin-bottom: 10pxborder-radius: 5pxbox-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px">select * from (select name, create_date, row_number() over (partition by name order by create_date desc) as r from t1) where r=1 </pre>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/6101847.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-14
下一篇 2023-03-14

发表评论

登录后才能评论

评论列表(0条)

保存