阅读过的博客
在MySQL中,要取每组前几条记录,可以使用子查询结合LIMIT子句来实现。但是,由于LIMIT是对结果集进行截取,不能直接在GROUP BY之后直接使用。因此,需要先对数据进行排序,然后再对每个分组的排序结果取前几条。
以下是一个示例代码,假设我们有一个表scores,包含group_id和score两个字段,我们想要获取每个group_id分组中score最高的前两条记录:
SELECT s1.*
FROM scores s1
JOIN (
SELECT group_id,
SUBSTRING_INDEX(
GROUP_CONCAT(score ORDER BY score DESC),
',',
2
) as scores
FROM scores
GROUP BY group_id
) s2 ON FIND_IN_SET(s1.score, s2.scores)
这段代码的工作原理是:
内部查询(s2)为每个group_id分组构建一个以逗号分隔的score列表,按score降序排列,并使用GROUP_CONCAT函数限制只取前两个。
外部查询(s1)通过FIND_IN_SET函数筛选出内部查询中标识的分数。
请注意,这种方法有其限制,例如,当分数非常接近时,可能会出现问题,或者如果分数列表非常长,可能会超出GROUP_CONCAT的长度限制。此外,对于大型数据集,这种方法可能效率不高,因为它涉及到字符串操作和JOIN操作。
1.MySQL超好用的函数合集(持续更新 https://zhuanlan.zhihu.com/p/258891026
2.高级sql https://blog.csdn.net/weixin_44390164/article/details/131492201?spm=1001.2014.3001.5502
https://www.cnblogs.com/xuhao0705/p/13902762.html
SQL的高级写法 https://blog.csdn.net/ACCPluzhiqi/article/details/131342342
https://hg95.github.io/DataBase/MySQL/%E7%AA%97%E5%8F%A3%E5%87%BD%E6%95%B0/%E5%88%86%E5%8C%BA%E5%87%BD%E6%95%B0Partition%20By.html
MySQL分区表最佳实践 https://juejin.cn/post/6844904174241447949
Mysql_窗口函数_OVER()的分组参数PARTITION BY https://blog.csdn.net/feizuiku0116/article/details/119843930
Mysql 分组聚合实现 over partition by 功能 https://www.cnblogs.com/zhwbqd/p/4205821.html
https://juejin.cn/post/7171412395955322888?searchId=20240426192757A70EDC6668BB6A9E73A7
静态代理和动态代理的区别 https://blog.csdn.net/weixin_43722048/article/details/121208939
静态代理和动态代理的区别和联系 https://www.cnblogs.com/baizhanshi/p/6611164.html
一篇吃透布隆过滤器(Bloom Filter)及其使用场景 https://blog.csdn.net/weixin_64366370/article/details/130693987
Java深度克隆的4种方法 https://www.cnblogs.com/baissy/p/15752382.html
真·Redis缓存优化实践——97%的优化率你见过嘛? https://juejin.cn/post/7275932704533577743?searchId=2024042619260521C4B13ED565599D539A
MySQL 中存储时间的最佳实践 https://juejin.cn/post/6979779882993582093
row_number() over partition by分组聚合 https://juejin.cn/post/7067003732969390094
MySQL 高级SQL语句(一) https://juejin.cn/post/7103961598242324517?from=search-suggest
MySQL 高级(进阶)SQL 语句 https://juejin.cn/post/7291952951047929868?searchId=20240426192757A70EDC6668BB6A9E73A7
MySQL中case when用法以及注意事项 https://juejin.cn/post/6971040309065187342?searchId=20240426192757A70EDC6668BB6A9E73A7
https://zhuanlan.zhihu.com/p/258891026
https://juejin.cn/post/7172905604791402504?searchId=20240426192757A70EDC6668BB6A9E73A7 「MySQL高级篇」MySQL为什么会死锁?
https://juejin.cn/post/6844903910142902286?searchId=20240426192757A70EDC6668BB6A9E73A7 深入理解mysql 索引特性(面试高频,屡试不爽的mysql索引总结)
「MySQL高级篇」详解MVCC https://juejin.cn/post/7171412395955322888?searchId=20240426192757A70EDC6668BB6A9E73A7
https://juejin.cn/post/7316540233549332515?searchId=20240426192757A70EDC6668BB6A9E73A7 MySQL索引优化:分页和join连接优化
https://blog.csdn.net/xct_xct/article/details/109332196 MySQL一些好用的函数
1. 七种方法查询字符串 https://www.jb51.net/database/301755udb.htm
2.mysql insert if not exists防止插入重复记录的方法 https://www.jb51.net/article/26934.htm
3. https://www.jb51.net/article/198339.htm
4. https://www.jb51.net/article/87870.htm
5.设置MySQL自动增长从某个指定的数开始方法 https://www.jb51.net/article/101862.htm
6.https://www.jb51.net/database/288814vte.htm
7.https://www.jb51.net/database/317574b0z.htm
8.Mysql查询不需要Group by的字段方法实例 https://www.jb51.net/database/320142hqc.htm
9.Mysql隐式类型转换方式 https://www.jb51.net/database/319228ivc.htm
10.sql count统计技巧 https://blog.51cto.com/u_15351403/3731060
11.MySQL数据库08——函数使用 https://blog.csdn.net/weixin_46277779/article/details/128980628
12.MySQL千万级数据从190秒优化到1秒的全过程 https://www.jb51.net/database/320112h8d.htm
13.MYSQL每隔10分钟进行分组统计的实现方法 https://www.jb51.net/article/100166.htm
14.mysql按天/小时/半小时/N分钟/分钟进行数据分组统计功能 https://www.jb51.net/database/320108w46.htm
15.MySQL实现模糊查询的高效方法总结(附30条优化建议) https://www.jb51.net/database/317574b0z.htm
16.MySQL外键约束的删除和更新总结 https://www.jb51.net/database/315724ihm.htm
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
17.MySQL数据库添加外键的四种方式 https://www.jb51.net/database/317423yja.htm
18.https://www.jb51.net/database/31909195t.htm#_lab2_2_9
19.MySQL INSERT INTO SELECT时自增Id不连续问题及解决 https://www.jb51.net/database/307352frq.htm
20.关于MySQL中“Insert into select“ 的死锁情况分析 https://www.jb51.net/database/285081w8j.htm
21.https://www.jb51.net/database/285081w8j.htm
22.https://blog.csdn.net/weixin_46277779/article/details/128980628
23.https://www.jb51.net/database/316215wb2.htm
24.设置MySQL自动增长从某个指定的数开始方法 https://www.jb51.net/article/101862.htm
25.mysql按天/小时/半小时/N分钟/分钟进行数据分组统计功能 https://www.jb51.net/database/320108w46.htm
小表驱动大表 https://blog.csdn.net/qq_38011415/article/details/103304189
小表驱动大表 https://blog.csdn.net/weixin_44390164/article/details/119970753
10个高级sql写法(高级) https://blog.csdn.net/bruce__ray/article/details/131144863
Sql 语句中 IN 和 EXISTS 的区别及应用(经典) https://blog.csdn.net/wqc19920906/article/details/79800374
mysql实现merge into的方法(经典): https://blog.csdn.net/weixin_39259170/article/details/115556393
MySQL四种方法实现merge into https://blog.csdn.net/weixin_43211480/article/details/126524257
https://blog.csdn.net/memory_hahaha/article/details/130832144
https://blog.csdn.net/xct_xct/article/details/109332196
mysql实现group by后取各分组的最新一条(经典) https://blog.csdn.net/HXNLYW/article/details/102681680
mysql分组取每组前几条记录(排名) 附group by与order by的研究(经典) https://www.cnblogs.com/mo-beifeng/archive/2012/02/07/2341886.html
https://blog.csdn.net/weixin_44606481/article/details/128200727?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-128200727-blog-102681680.235^v43^pc_blog_bottom_relevance_base5&spm=1001.2101.3001.4242.1&utm_relevant_index=3
SQL进阶:知乎 SQL高级编程语法实例(经典) https://zhuanlan.zhihu.com/p/113181518
https://blog.csdn.net/qq_24099547/article/details/96705482?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3-96705482-blog-115556393.235%5Ev43%5Epc_blog_bottom_relevance_base5&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3-96705482-blog-115556393.235%5Ev43%5Epc_blog_bottom_relevance_base5&utm_relevant_index=6
mysql一些随机函数记录
MySQL不常用但非常实用的函数在项目中的应用(太经典) https://blog.csdn.net/u011174699/article/details/131491198
(oracle)sql高级进阶 https://download.csdn.net/download/tonny1420/10148065?utm_medium=distribute.pc_relevant_download.none-task-download-2~default~BlogCommendFromBaidu~Rate-2-10148065-download-7636125.257%5Ev16%5Epc_dl_relevant_base1_c&depth_1-utm_source=distribute.pc_relevant_download.none-task-download-2~default~BlogCommendFromBaidu~Rate-2-10148065-download-7636125.257%5Ev16%5Epc_dl_relevant_base1_c&spm=1003.2020.3001.6616.2
1.mysql elt interval函数区间统计 https://www.cnblogs.com/zhaosq/p/17192256.html
2.MySQL常用函数详解,内含示例 https://baijiahao.baidu.com/s?id=1765488828100706520&wfr=spider&for=pc
3.MySQL常用函数大全(实例演示) https://blog.csdn.net/weixin_55076626/article/details/127341913
4. MySql常用函数全部汇总 https://www.cnblogs.com/panchanggui/p/10652322.html
5.索引失效的10种场景 https://blog.csdn.net/weixin_55076626/article/details/126416509#comments_31784340
6.MySQL中的any_value()函数 https://www.cnblogs.com/Uni-Hoang/p/17025504.html
https://blog.csdn.net/Oaklkm/article/details/127076598
Lambda——List的转化操作 https://blog.csdn.net/ybb_ymm/article/details/131069606
7.ava中使用Lambda表达式对集合排序 https://blog.csdn.net/mfysss/article/details/131208592?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-131208592-blog-134283230.235^v43^pc_blog_bottom_relevance_base5&spm=1001.2101.3001.4242.2&utm_relevant_index=4
synchronized能不能禁止指令重排序 https://blog.csdn.net/Hellowenpan/article/details/117750543
多态的概念以及详解 https://blog.csdn.net/weixin_57074076/article/details/131905336