MySQL分類排名和分組TOP N實例詳解
表結構
學生表如下:
CREATE TABLE `t_student` ( `id` int NOT NULL AUTO_INCREMENT, `t_id` int DEFAULT NULL COMMENT '學科id', `score` int DEFAULT NULL COMMENT '分數', PRIMARY KEY (`id`) );
數據如下:
題目一:獲取每個科目下前五成績排名(允許並列)
允許並列情況可能存在如4、5名成績並列情況,會導致取前4名得出5條數據,取前5名也是5條數據。
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名時
分析:
1.自身左外連接,得到所有的左邊值小於右邊值的集合。以t_id=1時舉例,24有5個成績大於他的(74、64、54、44、34),是第6名,34隻有4個成績大於他的,是第5名……74沒有大於他的,是第一名。
SELECT * FROM student s1 LEFT JOIN student s2 ON s1.t_id = s2.t_id AND s1.score < s2.score
2. 把總結的規律轉換成SQL表示出來,就是group by 每個student 的 id(s1.id),Having統計這個id下面有多少個比他大的值(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. 最後根據 t_id 分類,score 倒序排序即可。
題目二:獲取每個科目下最後兩名學生的成績平均值
取最後兩名成績
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
並列存在情況下可能導致篩選出的同一t_id 下結果條數大於2條,但題目要求是取最後兩名的平均值,多條平均後還是本身,故不必再對其處理,可以滿足題目要求。
分組求平均值:
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
結果:
分析:
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 計數取2條
3. group by t_id 分別取各自學科的然後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.引入輔助參數
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.去除重復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條不重復的
總結
到此這篇關於MySQL分類排名和分組TOP N實例詳解的文章就介紹到這瞭,更多相關MySQL分類排名 TOP N內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!