score表:
字段 student_id 學生 id ,class_id:班級 id ,score:分數create
table
`score` (
`student_id`
int(10) default
null,
`class_id`
int(10) default
null,
`score`
int(5) default
null
) engine=innodb default charset=utf8mb4 collate=utf8mb4_unicode_ci
資料準備:
表結構如下:insert
into score values(1,1,100),(2,1,93),(3,1,89),(4,1,96),(5,2,98),(6,2,97),(7,2,90),(8,2,88),(9,1,96);
1.取每個班級前兩名的學生(包含並列第二名)mysql> select * from score;
+------------+----------+-------+
| student_id | class_id | score |
+------------+----------+-------+
| 1 | 1 | 100 |
| 2 | 1 | 93 |
| 3 | 1 | 89 |
| 4 | 1 | 96 |
| 5 | 2 | 98 |
| 6 | 2 | 97 |
| 7 | 2 | 90 |
| 8 | 2 | 88 |
| 9 | 1 | 96 |
+------------+----------+-------+
9 rows in set (0.00 sec)
sql 解釋:取表 s1的資料,這些資料中 class_id 和 s2 class_id相同的資料下,比 s1的 score 分數大的 s2的資料條目必須小於2mysql> select * from score s1 where (select count(0) from score s2 where s1.class_id = s2.class_id and s1.score < s2.score) < 2;
+------------+----------+-------+
| student_id | class_id | score |
+------------+----------+-------+
| 1 | 1 | 100 |
| 4 | 1 | 96 |
| 5 | 2 | 98 |
| 6 | 2 | 97 |
| 9 | 1 | 96 |
+------------+----------+-------+
5 rows in set (0.00 sec)
或者使用 left join 的方式:
2.取學生分數資料且表示排名mysql> select s1.* from score s1 left join score s2 on s1.class_id = s2.class_id and s1.score| student_id | class_id | score |
+------------+----------+-------+
| 1 | 1 | 100 |
| 4 | 1 | 96 |
| 9 | 1 | 96 |
| 5 | 2 | 98 |
| 6 | 2 | 97 |
+------------+----------+-------+
5 rows in set (0.00 sec)
sql解釋:將 s2中比s1中分數大的條目顯示出來就行了(count 時需要加1)mysql> select s1.*,(select count(0) + 1 from score s2 where s2.score > s1.score)rank from score s1;
+------------+----------+-------+------+
| student_id | class_id | score | rank |
+------------+----------+-------+------+
| 1 | 1 | 100 | 1 |
| 2 | 1 | 93 | 6 |
| 3 | 1 | 89 | 8 |
| 4 | 1 | 96 | 4 |
| 5 | 2 | 98 | 2 |
| 6 | 2 | 97 | 3 |
| 7 | 2 | 90 | 7 |
| 8 | 2 | 88 | 9 |
| 9 | 1 | 96 | 4 |
+------------+----------+-------+------+
9 rows in set (0.00 sec)
3.取學生成績資料,表示班級排名
與之前一樣,但過濾條件中只需要計算班級相同的資料條目mysql> select s1.*,(select count(0) + 1 from score s2 where s1.class_id = s2.class_id and s2.score > s1.score)rank from score s1 order by class_id,rank;
+------------+----------+-------+------+
| student_id | class_id | score | rank |
+------------+----------+-------+------+
| 1 | 1 | 100 | 1 |
| 4 | 1 | 96 | 2 |
| 9 | 1 | 96 | 2 |
| 2 | 1 | 93 | 4 |
| 3 | 1 | 89 | 5 |
| 5 | 2 | 98 | 1 |
| 6 | 2 | 97 | 2 |
| 7 | 2 | 90 | 3 |
| 8 | 2 | 88 | 4 |
+------------+----------+-------+------+
9 rows in set (0.00 sec)
4.取每個班級前兩名(並列的只取前面的資料)
參考文件:
學生成績排名
表結構t2 sid fs 1 200 2 200 3 190 4 190 5 180 sid為學號,fs為學生總分 要求結果為 sid fs paiming 1 200 1 2 200 1 3 190 2 4 190 2 5 180 3 測試資料 create table t2 sid int nu...
學生成績排名
我做學校辦公管理的專案過程中,需要學生成績排名。學生的成績排名有個特點,就是相同分數,名次相同,但後面的排名要按前面的人數順次排下去。如有兩個第三名,那麼接下來就是第五名了。下面是自己做的測試 public void pxout 排序方法 collections.sort infoids,new c...
學生成績排名(並列問題)
成績排名 輸入多個學生的姓名和成績,列印其名稱.成績和排名 輸入 第一行 整數n 表示有n個學生 第二行開始,每行乙個字串和乙個整數,表示學生姓名和成績 輸出 按成績從高到低的順序列印每個學生的姓名,成績,排名 需要注意的是,如果成績相同,則排名並列 include include include ...