Rumah > Artikel > pangkalan data > MySQL实现Oracle或者PostgreSQL的row_numberover这样的排名语法_MySQL
bitsCN.com
PostgreSQL 和Oracle 都提供了 row_number() over() 这样的语句来进行对应的字段排名, 很是方便。 MySQL却没有提供这样的语法。
Table "ytt.t1" Column | Type | Modifiers --------+-----------------------+----------- i_name | character varying(10) | not null rank | integer | not null
t_girl=# select * from t1 order by i_name; i_name | rank ---------+------ Charlie | 12 Charlie | 12 Charlie | 13 Charlie | 10 Charlie | 11 Lily | 6 Lily | 7 Lily | 7 Lily | 6 Lily | 5 Lily | 7 Lily | 4 Lucy | 1 Lucy | 2 Lucy | 2 Ytt | 14 Ytt | 15 Ytt | 14 Ytt | 14 Ytt | 15 (20 rows)t_girl=# select i_name,rank, row_number() over(partition by i_name order by rank desc) as rank_number from t1; i_name | rank | rank_number ---------+------+------------- Charlie | 13 | 1 Charlie | 12 | 2 Charlie | 12 | 3 Charlie | 11 | 4 Charlie | 10 | 5 Lily | 7 | 1 Lily | 7 | 2 Lily | 7 | 3 Lily | 6 | 4 Lily | 6 | 5 Lily | 5 | 6 Lily | 4 | 7 Lucy | 2 | 1 Lucy | 2 | 2 Lucy | 1 | 3 Ytt | 15 | 1 Ytt | 15 | 2 Ytt | 14 | 3 Ytt | 14 | 4 Ytt | 14 | 5 (20 rows)
t_girl=# select i_name,rank, row_number() over(partition by i_name ) as rank_number from t1; i_name | rank | rank_number ---------+------+------------- Charlie | 12 | 1 Charlie | 12 | 2 Charlie | 13 | 3 Charlie | 10 | 4 Charlie | 11 | 5 Lily | 6 | 1 Lily | 7 | 2 Lily | 7 | 3 Lily | 6 | 4 Lily | 5 | 5 Lily | 7 | 6 Lily | 4 | 7 Lucy | 1 | 1 Lucy | 2 | 2 Lucy | 2 | 3 Ytt | 14 | 1 Ytt | 15 | 2 Ytt | 14 | 3 Ytt | 14 | 4 Ytt | 15 | 5(20 rows)
t_girl=# select i_name,rank, row_number() over() as rank_number from t1; i_name | rank | rank_number ---------+------+------------- Lily | 7 | 1 Lucy | 2 | 2 Ytt | 14 | 3 Ytt | 14 | 4 Charlie | 12 | 5 Charlie | 13 | 6 Lily | 7 | 7 Lily | 4 | 8 Ytt | 14 | 9 Lily | 6 | 10 Lucy | 1 | 11 Lily | 7 | 12 Ytt | 15 | 13 Lily | 6 | 14 Charlie | 11 | 15 Charlie | 12 | 16 Lucy | 2 | 17 Charlie | 10 | 18 Lily | 5 | 19 Ytt | 15 | 20(20 rows)
CALL sp_rownumber('t1','i_name','rank','desc');query resulti_name rank rownumCharlie 13 1Charlie 12 2Charlie 12 3Charlie 11 4Charlie 10 5Lily 7 1Lily 7 2Lily 7 3Lily 6 4Lily 6 5Lily 5 6Lily 4 7Lucy 2 1Lucy 2 2Lucy 1 3Ytt 15 1Ytt 15 2Ytt 14 3Ytt 14 4Ytt 14 5
CALL sp_rownumber('t1','i_name',NULL,NULL);query resulti_name rank rownumCharlie 12 1Charlie 13 2Charlie 11 3Charlie 12 4Charlie 10 5Lily 7 1Lily 7 2Lily 4 3Lily 6 4Lily 7 5Lily 6 6Lily 5 7Lucy 2 1Lucy 1 2Lucy 2 3Ytt 14 1Ytt 14 2Ytt 14 3Ytt 15 4Ytt 15 5
CALL sp_rownumber('t1',NULL,NULL,NULL);query resulti_name rank rownumLily 7 1Lucy 2 2Ytt 14 3Ytt 14 4Charlie 12 5Charlie 13 6Lily 7 7Lily 4 8Ytt 14 9Lily 6 10Lucy 1 11Lily 7 12Ytt 15 13Lily 6 14Charlie 11 15Charlie 12 16Lucy 2 17Charlie 10 18Lily 5 19Ytt 15 20
bitsCN.com