mysql 数据库group by 按排序取值

mysql 数据库group by 按排序取值 

mysql 5.7 以后,group by 想要按照排序的顺序来取值,在子结果集中要有union,group by 等操作,不然按照id默认排序来取值

示例:

数据库:

score表

id  user_id  num

1  100         95

2  100         96

3  101         97

4  101         95

select * from score group by user_id order by num desc

结果

id  user_id  num

3  101         97

1  100         95

select * from 

  (select * from score group by id order by num desc) f

group by user_id order by num desc

结果

id  user_id  num

3  101         97

2  100         96

评论

暂无评论