mysql使用自定義序列實(shí)現(xiàn)row_number功能(步驟詳解)
看了一些文章,終于知道該怎么在 mysql 里面實(shí)現(xiàn) row_number() 排序
話不多說(shuō),show you the code:
第一步:建表:
create table grades( `name` varchar(10), `subject` varchar(10), `score` int(10) )
第二步:寫入數(shù)據(jù)
insert into grades(name, subject, score)
values('小明', '語(yǔ)文', 85),
('小華', '語(yǔ)文', 89),
('小李', '語(yǔ)文', 91),
('小芳', '語(yǔ)文', 93),
('小明', '數(shù)學(xué)', 77),
('小華', '數(shù)學(xué)', 95),
('小李', '數(shù)學(xué)', 83),
('小芳', '數(shù)學(xué)', 88),
('小明', '英語(yǔ)', 90),
('小華', '英語(yǔ)', 92),
('小李', '英語(yǔ)', 85),
('小芳', '英語(yǔ)', 88)
數(shù)據(jù)如下:

第三步:
需求:找出各科目單科第二的同學(xué)
首先,先排序:
select name, subject, score from grades order by subject, score desc
數(shù)據(jù)如下:

然后,每個(gè)科目按照分組排序
select (@i:=case when @subject_pre=t1.subject then @i+1 else 1 end) as rn,
t1.*,
(@subject_pre:=subject)
from (
select name, subject, score
from grades
order by subject, score desc
) t1,
(select @i:=0, @subject_pre:='') as t2
group by subject, score
order by subject, score desc

解釋一下:
添加一個(gè)比較項(xiàng) subject_pre, 記錄前一個(gè)科目是什么。
再加上一個(gè)自增的序列,實(shí)現(xiàn)index+1的功能。
因?yàn)閿?shù)據(jù)已經(jīng)是有序的,如果指向的科目和存儲(chǔ)的前一個(gè)科目相同,那么序號(hào)+1,否則的話,序號(hào)從1開始重新計(jì)算。
這樣就實(shí)現(xiàn)了分組排序。
最后,把 rn=2 的數(shù)據(jù)取出來(lái)
select name, subject, score from( select (@i:=case when @subject_pre=t1.subject then @i+1 else 1 end) as rn, t1.name, t1.subject, t1.score, (@subject_pre:=subject) from ( select name, subject, score from grades order by subject, score desc ) t1, (select @i:=0, @subject_pre:='') as t2 group by subject, score order by subject, score desc ) t where rn=2
最后結(jié)果如下:

這樣就使用mysql實(shí)現(xiàn)了row_number()的功能。
在網(wǎng)上找的資料,很多沒寫清楚,這里特地用一個(gè)示例把這個(gè)實(shí)現(xiàn)講清楚了,希望對(duì)你有幫助!
到此這篇關(guān)于mysql使用自定義序列實(shí)現(xiàn)row_number功能的文章就介紹到這了,更多相關(guān)mysql?row_number功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL存儲(chǔ)過程的創(chuàng)建使用以及實(shí)現(xiàn)數(shù)據(jù)快速插入
因最近想要測(cè)試一下MySQL百萬(wàn)級(jí)數(shù)據(jù)處理過程,所以要一次對(duì)數(shù)據(jù)庫(kù)快速插入大量數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MySQL存儲(chǔ)過程的創(chuàng)建使用以及實(shí)現(xiàn)數(shù)據(jù)快速插入的相關(guān)資料,需要的朋友可以參考下2023-03-03
關(guān)于MySQL主從復(fù)制的幾種復(fù)制方式總結(jié)
這篇文章主要給大家介紹了關(guān)于MySQL主從復(fù)制的幾種復(fù)制方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
insert into … on duplicate key update / replace into 多行數(shù)據(jù)介紹
當(dāng)我插入一條數(shù)據(jù)時(shí),我要判斷(k1,k2)是否已經(jīng)存在(1條selete),若存在就update,不存在就insert2013-08-08
解決MySQL:Invalid GIS data provided to&nbs
這篇文章主要介紹了解決MySQL:Invalid GIS data provided to function st_geometryfromtext問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
MySQL中SQL分頁(yè)查詢的幾種實(shí)現(xiàn)方法及優(yōu)缺點(diǎn)
這篇文章主要介紹了MySQL中SQL分頁(yè)查詢的幾種實(shí)現(xiàn)方法及優(yōu)缺點(diǎn), 分頁(yè)查詢就是在滿足條件的一堆有序數(shù)據(jù)中截取當(dāng)前所需要展示的那部分。對(duì)此感興趣的可以來(lái)了解一下2020-07-07
mysql通過frm和ibd文件恢復(fù)表_mysql5.7根據(jù).frm和.ibd文件恢復(fù)表結(jié)構(gòu)和數(shù)據(jù)
文章主要介紹了如何從.frm和.ibd文件恢復(fù)MySQL InnoDB表結(jié)構(gòu)和數(shù)據(jù),需要的朋友可以參考下2025-03-03

