Mysql @RN :方式ROW_NUMBER的实现方法
目錄
1簡介... 1
2 準備... 1
2.1 環境說明... 1
3 安裝... 2
4 配置... 2
5 使用... 2
5.1 驗證實驗... 2
5.2 問題分析... 4
6 延展... 4
7 總結... 4
Mysql @RN :方式ROW_NUMBER的實現方法
1簡介
基于mysql @rownum:=@rownum+1的方式模擬實現row_number
2 準備
2.1 環境說明
1 mysql版本10.3.13-MariaDB,同理其它mysql版本效果類似。
-- 查看mysql版本 SELECT VERSION() AS mysql_Version;| mysql_Version |
| 10.3.13-MariaDB |
| Variable_name | Value |
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | E:\Program Files\MariaDB 10.3\share\charsets\ |
3 安裝
無.
4 配置
無
5 使用
5.1 驗證實驗
1) mysql里創建一個emp_t10表定義empid ,deptid ,salary字段的類型,插入如下10條數據驗證。
-- 1 創建表并插入數據 drop table if exists emp_t10; create table emp_t10 (empid int ,deptid int ,salary decimal(10,2) );insert into emp_t10 values (1,10,5500.00), (2,10,4500.00), (3,20,1900.00), (4,20,4800.00), (5,40,6500.00), (6,40,14500.00), (7,40,44500.00), (8,50,6500.00), (9,50,7500.00),(10, 20,1900.00 );-- 2 將按照部門升序、工資降序的數據插入臨時表中 create table emp_t10_tmp AS select empid,deptid,salary from emp_t10 order by deptid asc ,salary desc; -- 3 按照deptid分組,按照salary降序排名 select empid,deptid,salary,rank from ( select heyf_tmp.empid,heyf_tmp.deptid,heyf_tmp.salary,@rownum := @rownum+1 , if(@pdept= heyf_tmp.deptid,@rank:=@rank +1, @rank:= 1) as rank, @pdept:=heyf_tmp.deptid from ( -- select empid,deptid,salary from emp_t10 order by deptid asc ,salary desc select empid,deptid,salary from emp_t10_tmp) heyf_tmp ,(select @rownum:=0 , @pdept:= null ,@rank:= 0) a ) result order by deptid,rank;-- 4 建議的寫法,按照deptid分組,按照salary降序排名(這里沒用利用臨時表,order by 在兩個表cross join之后) select empid,deptid,salary,rank from ( select heyf_tmp.empid,heyf_tmp.deptid,heyf_tmp.salary,@rownum := @rownum+1 , if(@pdept= heyf_tmp.deptid,@rank:=@rank +1, @rank:= 1) as rank, @pdept:=heyf_tmp.deptid from ( select empid,deptid,salary from emp_t10 -- order by deptid asc ,salary desc -- select empid,deptid,salary from emp_t10_tmp) heyf_tmp ,(select @rownum:=0 , @pdept:= null ,@rank:= 0) a order by deptid asc ,salary desc ) result order by deptid,rank;| empid | deptid | salary | rank |
| 1 | 10 | 5500.00 | 1 |
| 2 | 10 | 4500.00 | 2 |
| 10 | 20 | 1900.00 | 2 |
| 3 | 20 | 1900.00 | 3 |
| 4 | 20 | 4800.00 | 1 |
| 7 | 40 | 44500.00 | 1 |
| 6 | 40 | 14500.00 | 2 |
| 5 | 40 | 6500.00 | 3 |
| 8 | 50 | 6500.00 | 2 |
| 9 | 50 | 7500.00 | 1 |
2) 當然在當前的MariaDB 版本里已經支持了ROW_NUMER,所以我們可以直接按照如下的語法實現想要的需求:
select empid,deptid,salary, ROW_NUMBER() OVER(PARTITION BY deptid ORDER BY salary DESC)rank?? from emp_t10;| empid | deptid | salary | rank |
| 1 | 10 | 5500.00 | 1 |
| 2 | 10 | 4500.00 | 2 |
| 4 | 20 | 4800.00 | 1 |
| 10 | 20 | 1900.00 | 2 |
| 3 | 20 | 1900.00 | 3 |
| 7 | 40 | 44500.00 | 1 |
| 6 | 40 | 14500.00 | 2 |
| 5 | 40 | 6500.00 | 3 |
| 9 | 50 | 7500.00 | 1 |
| 8 | 50 | 6500.00 | 2 |
5.2 問題分析
無
6 延展
無
7 總結
暫無.有問題可以咨詢如下:
總結
以上是生活随笔為你收集整理的Mysql @RN :方式ROW_NUMBER的实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从从长春东方广场坐地铁2号线路车去百花园
- 下一篇: 汽车机电维修工资待遇?