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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM