您好,登錄后才能下訂單哦!
這篇文章主要介紹MySQL中分類(lèi)排名和分組TOP N的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
學(xué)生表如下:
CREATE TABLE `t_student` ( `id` int NOT NULL AUTO_INCREMENT, `t_id` int DEFAULT NULL COMMENT '學(xué)科id', `score` int DEFAULT NULL COMMENT '分?jǐn)?shù)', PRIMARY KEY (`id`) );
數(shù)據(jù)如下:
允許并列情況可能存在如4、5名成績(jī)并列情況,會(huì)導(dǎo)致取前4名得出5條數(shù)據(jù),取前5名也是5條數(shù)據(jù)。
SELECT s1.* FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score < s2.score GROUP BY s1.id HAVING COUNT( s2.id ) < 5 ORDER BY s1.t_id, s1.score DESC
ps:取前4名時(shí)
1.自身左外連接,得到所有的左邊值小于右邊值的集合。以t_id=1時(shí)舉例,24有5個(gè)成績(jī)大于他的(74、64、54、44、34),是第6名,34只有4個(gè)成績(jī)大于他的,是第5名......74沒(méi)有大于他的,是第一名。
SELECT * FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score < s2.score
2. 把總結(jié)的規(guī)律轉(zhuǎn)換成SQL表示出來(lái),就是group by 每個(gè)student 的 id(s1.id),Having統(tǒng)計(jì)這個(gè)id下面有多少個(gè)比他大的值(s2.id)
SELECT s1.* FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score < s2.score GROUP BY s1.id HAVING COUNT( s2.id ) < 5
3. 最后根據(jù) t_id 分類(lèi),score 倒序排序即可。
取最后兩名成績(jī)
SELECT s1.* FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score > s2.score GROUP BY s1.id HAVING COUNT( s1.id )< 2 ORDER BY s1.t_id, s1.score
并列存在情況下可能導(dǎo)致篩選出的同一t_id 下結(jié)果條數(shù)大于2條,但題目要求是取最后兩名的平均值,多條平均后還是本身,故不必再對(duì)其處理,可以滿足題目要求。
分組求平均值:
SELECT t_id,AVG(score) FROM ( SELECT s1.* FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score > s2.score GROUP BY s1.id HAVING COUNT( s1.id )< 2 ORDER BY s1.t_id, s1.score ) tt GROUP BY t_id
結(jié)果:
1. 查詢出所有t1.score>t2.score 的記錄
SELECT s1.*,s2.* FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score > s2.score
2. group by s.id 去重,having 計(jì)數(shù)取2條
3. group by t_id 分別取各自學(xué)科的然后avg取均值
SELECT * FROM ( SELECT s1.*, @rownum := @rownum + 1 AS num_tmp, @incrnum := CASE WHEN @rowtotal = s1.score THEN @incrnum WHEN @rowtotal := s1.score THEN @rownum END AS rownum FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score > s2.score, ( SELECT @rownum := 0, @rowtotal := NULL, @incrnum := 0 ) AS it GROUP BY s1.id ORDER BY s1.t_id, s1.score DESC ) tt GROUP BY t_id, score, rownum HAVING COUNT( rownum )< 5
1.引入輔助參數(shù)
SELECT s1.*, @rownum := @rownum + 1 AS num_tmp, @incrnum := CASE WHEN @rowtotal = s1.score THEN @incrnum WHEN @rowtotal := s1.score THEN @rownum END AS rownum FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score > s2.score, ( SELECT @rownum := 0, @rowtotal := NULL, @incrnum := 0 ) AS it
2.去除重復(fù)s1.id,分組排序
SELECT s1.*, @rownum := @rownum + 1 AS num_tmp, @incrnum := CASE WHEN @rowtotal = s1.score THEN @incrnum WHEN @rowtotal := s1.score THEN @rownum END AS rownum FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score > s2.score, ( SELECT @rownum := 0, @rowtotal := NULL, @incrnum := 0 ) AS it GROUP BY s1.id ORDER BY s1.t_id, s1.score DESC
3.GROUP BY t_id, score, rownum 然后 HAVING 取前5條不重復(fù)的
以上是“MySQL中分類(lèi)排名和分組TOP N的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。