婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > mysql獲取分組后每組的最大值實例詳解

mysql獲取分組后每組的最大值實例詳解

熱門標簽:ps制作地圖標注gif 成都優派外呼系統 電子地圖標注電話 地圖標注人員兼職 聊城智能外呼系統運營商 寧波企業外呼系統收費 扎樣申請400電話 沈陽電銷外呼系統原理是什么 上海智能外呼系統代理商

 mysql獲取分組后每組的最大值實例詳解

1. 測試數據庫表如下:

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. 插入如下數據:

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. 下面是重點,目的是要按照name分組,然后分組后,獲取每組中score分數最多的,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; 

當然,上面的最后的order by a.name可以去掉

4. 測試結果如下:

+----+----------+-------+ 
| 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. 網上很多方法都是錯誤的,比如如下一些,親測是不行的

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; 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:
  • MySql分組后隨機獲取每組一條數據的操作
  • 基于mysql實現group by取各分組最新一條數據
  • MySQL 子查詢和分組查詢
  • MySQL 分組查詢和聚合函數
  • MySql Group By對多個字段進行分組的實現方法
  • 詳解MySQL分組排序求Top N
  • Mysql利用group by分組排序
  • 詳解MySQL 數據分組

標簽:宿州 汕頭 林芝 AXB 三明 咸寧 朔州 內江

巨人網絡通訊聲明:本文標題《mysql獲取分組后每組的最大值實例詳解》,本文關鍵詞  mysql,獲取,分組,后,每組,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《mysql獲取分組后每組的最大值實例詳解》相關的同類信息!
  • 本頁收集關于mysql獲取分組后每組的最大值實例詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 哈巴河县| 钟山县| 安康市| 蒙自县| 霞浦县| 曲沃县| 二连浩特市| 泸西县| 商洛市| 灌云县| 咸宁市| 望奎县| 景洪市| 如东县| 夏邑县| 定兴县| 班戈县| 乌鲁木齐县| 滕州市| 上虞市| 南郑县| 苍山县| 罗甸县| 富川| 五指山市| 康保县| 巴塘县| 同仁县| 遂溪县| 德州市| 富宁县| 巴中市| 乌审旗| 历史| 鹿泉市| 鲁甸县| 佛学| 汝城县| 盐亭县| 德江县| 德安县|