學生表格如下:
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 DESCps:取前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.score2. 把總結的規律轉換成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 ) < 53. 最後依照 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.score2. 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
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 DESC3.GROUP BY t_id, score, rownum 接著HAVING 取前5條不重複的
以上是MySQL中分類排名與分組TOP N的範例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!