https://blog.csdn.net/persistencegoing/article/details/92764058
All rights reserved.No part of this article may be reproduced or distributed by any means,or stored in a database or retrieval system,without the prior written permission of persistenceGoing author
https://blog.csdn.net/persistencegoing/article/details/84376427
有一个学生分数表student,数据结构是这样的
id(当前表ID) student_id(学生ID) line(分数) subject_type(科目类型)
1 1 80 1
2 1 78 1
3 1 56 1
4 2 99 1
5 2 20 1
我想根据学生分组 ,得到每个分组中分数最高的那一条数据:
先来几种错误的写法:
(1)SELECT
max.*, MAX(max.line) lineMax
FROM
(SELECT * FROM student) max
GROUP BY
student_id
这种写法你会发现line和lineMax的列中数据可能不一致
(2)SELECT
max.*, MAX(max.line) lineMax
FROM
(
SELECT
*
FROM
student
ORDER BY
line DESC
) max
GROUP BY
student_id
你可能会说排序之后再分组就对了,抱歉,这种情况下对于某些小伙伴可能还是会有问题
(3)SELECT
max.*, MAX(max.line) lineMax
FROM
(
SELECT
*
FROM
student
ORDER BY
line DESC
) max
GROUP BY
student_id
HAVING lineMax=line
这种情况下,表面上数据是对的,但是数据量大了之后你会发现有些数据丢失,其实还是错的
正确写法:
SELECT
max.*, MAX(max.line) lineMax
FROM
(
SELECT
*
FROM
student
ORDER BY
line DESC
LIMIT 100
) max
GROUP BY
student_id
在mysql5.7版本之后,必须再排序之后加上limit关键字,20万所测试的数据是正确的,亲测验证
注意:
limit 是必须要加的,如果不加的话,数据不会先进行排序,通过 explain 查看执行计划,可以看到没有 limit 的时候,少了一个 DERIVED 操作。
可用以下SQL对每组数据进行验证
SELECT
*
FROM
student
WHERE
student_id = 69
ORDER BY
line DESC
LIMIT 100
————————————————
版权声明:本文为CSDN博主「persistenceヾ(?°∇°?)?」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/persistencegoing/article/details/92764058