mysql獲取分組后每組的最大值實(shí)例詳解
mysql獲取分組后每組的最大值實(shí)例詳解
1. 測(cè)試數(shù)據(jù)庫(kù)表如下:
create table test ( `id` int not null auto_increment, `name` varchar(20) not null default '', `score` int not null default 0, primary key(`id`) )engine=InnoDB CHARSET=UTF8;
2. 插入如下數(shù)據(jù):
mysql> select * from test; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | jason | 1 | | 2 | jason | 2 | | 3 | jason | 3 | | 4 | linjie | 1 | | 5 | linjie | 2 | | 6 | linjie | 3 | | 7 | xiaodeng | 1 | | 8 | xiaodeng | 2 | | 9 | xiaodeng | 3 | | 10 | hust | 2 | | 11 | hust | 3 | | 12 | hust | 1 | | 13 | haha | 1 | | 14 | haha | 2 | | 15 | dengzi | 3 | | 16 | dengzi | 4 | | 17 | dengzi | 5 | | 18 | shazi | 3 | | 19 | shazi | 4 | | 20 | shazi | 2 | +----+----------+-------+
3. 下面是重點(diǎn),目的是要按照name分組,然后分組后,獲取每組中score分?jǐn)?shù)最多的,sql如下
select a.* from test a inner join (select name,max(score) score from test group by name)b on a. name=b.name and a.score=b.score order by a.name;
當(dāng)然,上面的最后的order by a.name可以去掉
4. 測(cè)試結(jié)果如下:
+----+----------+-------+ | id | name | score | +----+----------+-------+ | 3 | jason | 3 | | 6 | linjie | 3 | | 9 | xiaodeng | 3 | | 11 | hust | 3 | | 14 | haha | 2 | | 17 | dengzi | 5 | | 19 | shazi | 4 | +----+----------+-------+
5. 網(wǎng)上很多方法都是錯(cuò)誤的,比如如下一些,親測(cè)是不行的
select * from (select * from test order by score desc) t group by name order by score desc limit 4; select score,max(score) from test group by name; select * from test where score in (select max(score) from test group by name); select * from test where score in (select substring_index(group_concat(score order by score desc separator ','),',',1) from test group by name); select * from (select name,score,ROW_NUMBER() over(group by name order by score desc) as rowNum from test) rank where rank.rowNum <=1 order by rank.score desc; select * from( select StoresNo,[CustomerCaseNo],[PaymentsTime], ROW_NUMBER() over(partition by CustomerCaseNo order by [PaymentsTime] desc) as rowNum from BAL_paymentsSwiftInfo where StoresNo='zq00000034') ranked where ranked.rowNum <= 1 order by ranked.CustomerCaseNo, ranked.PaymentsTime desc select * from (select * from test order by score desc) as a group by a.name;
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
MySQL登錄時(shí)出現(xiàn)ERROR 1045: Access denied for&
本文已解決MySQL登錄時(shí)出現(xiàn)Access denied for user ‘root‘@‘localhost‘ (using password: YES)無(wú)法打開的相關(guān)報(bào)錯(cuò)問(wèn)題,并總結(jié)提出了幾種可用解決方案,又遇到同樣問(wèn)題的朋友可以參考閱讀下本文2024-09-09MySQL數(shù)據(jù)庫(kù)執(zhí)行Update卡死問(wèn)題的解決方法
最近開發(fā)的時(shí)候debug到一條update的sql語(yǔ)句時(shí)程序就不動(dòng)了,然后我就在plsql上試了一下,發(fā)現(xiàn)plsql一直在顯示正在執(zhí)行,等了好久也不出結(jié)果,下面這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫(kù)執(zhí)行Update卡死問(wèn)題的解決方法,需要的朋友可以參考下2022-05-05mybatis-plus分頁(yè)傳入?yún)?shù)后sql where條件沒有l(wèi)imit分頁(yè)信息操作
這篇文章主要介紹了mybatis-plus分頁(yè)傳入?yún)?shù)后sql where條件沒有l(wèi)imit分頁(yè)信息操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11美團(tuán)網(wǎng)技術(shù)團(tuán)隊(duì)分享的MySQL索引及慢查詢優(yōu)化教程
這篇文章主要介紹了美團(tuán)網(wǎng)技術(shù)團(tuán)隊(duì)分享的MySQL索引及慢查詢優(yōu)化教程,結(jié)合了實(shí)際的磁盤IO情況對(duì)一些優(yōu)化方案作出了分析,十分推薦!需要的朋友可以參考下2015-11-11