1. 程式人生 > >mysql學生成績排名,分組取前 N 條記錄

mysql學生成績排名,分組取前 N 條記錄

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

欄位 student_id 學生 id ,class_id:班級 id ,score:分數
資料準備:

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);

表結構如下:

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)

1.取每個班級前兩名的學生(包含並列第二名)

mysql> 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)

sql 解釋:取表 s1的資料,這些資料中 class_id 和 s2 class_id相同的資料下,比 s1的 score 分數大的 s2的資料條目必須小於2

或者使用 left join 的方式:

mysql> select s1.* from score s1 left join score s2 on s1.class_id = s2.class_id and s1.score<s2.score group by s1.class_id,s1.student_id,s1.score having count(s2.student_id)<2;
+------------+----------+-------+
| 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)

2.取學生分數資料且表示排名

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)

sql解釋:將 s2中比s1中分數大的條目顯示出來就行了(count 時需要加1)

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.取每個班級前兩名(並列的只取前面的資料)

相關推薦

mysql學生成績排名,分組 N 記錄

score表: CREATE TABLE `score` ( `student_id` int(10) DEFAULT NULL, `class_id` int(10) DEFAULT NULL, `score` int(5) DEFAULT

mysql使用GROUP BY分組實現N記錄的方法

cls class ges rom 當前 分組 實現 一個 images MySQL中GROUP BY分組取前N條記錄實現 mysql分組,取記錄 GROUP BY之後如何取每組的前兩位下面我來講述mysql中GROUP BY分組取前N條記錄實現方法。 這是測試表(也

Mysql分組統計、排序、N記錄解決方案

今日根據專案需求,需要在mysql中解決記錄的分組統計、排序,並抽取前10條記錄的功能。現已解決,解決方案如下: 1)表結構 <span style="font-size:18px;">CREATE TABLE `policy_keywords_rel` (

Linq分組及排序,N記錄

Linq多欄位分組排序並取前N條記錄時,一定要先分組再排序,不然取到的記錄是不規則的 程式碼示例【按HotWord分組,並取sorNum倒序,取前15條記錄】 [Route("api/

Mysql和Oracl 分組每組N記錄

MySQL取每組的前N條記錄:使用自連線的方式一、對分組的記錄取前N條記錄:例子:取前 2條最大(小)的記錄1234567891011121314151617181.用子查詢:SELECT * FROM right2 a  WHERE 2>(SELECT COUNT(*

MySQL每組的N記錄

  MySQL 分組後取每組前N條資料 與oracle的 rownumber() over(partition by xxx  order by xxx )語句類似,即:對錶分組後排序 CREATE TABLE `mygoods` ( `goods

分組查詢每組n記錄例項

假設有這樣一張運動員比賽成績表 tb_score 現在要求查詢出每個國家的前三名的成績記錄,查詢語句可以這樣寫: 1、 select t3.id,t3.country,t3.score fro

MySql按字段分組最大值記錄 [此博文包含圖片]

from htm 技術 方法 sin gid 最大值 count not 要求:獲得按table1_id分組,並且age最大的記錄信息,即2、3、5條 方法一: select * from (select * from table2 order by age

Oracle 中sql語句中的n數據

沒有 blog tab 註意 sql table bsp num 數據 取得薪水最高的前五名員工 sql: select * from ( select empno,ename,sal from emp order by sal desc) where rownum&l

parttion by ~~~針對某個欄位或多個欄位重複,資料只n。問題例子:1.主評論下的評論按著 時間正序最多隻5 2.獲取最新登入記錄......

分組目前已知partition by、group by partition by用於給結果集分組分割槽,如果沒有指定那麼它把整個結果集作為一個分組,最後顯示具體資料 group by:通過所查詢的資料的某一欄位或屬性進行分組,最後顯示組資料,而不是具體資料,因為select 後面的所有列中,沒有使用聚合函

sql語句分組查詢排序後取出n記錄

1.sql語句分組排序後取出前n條記錄 (SELECT * FROM ( SELECT "row_number" () OVER ( PARTITION BY mmsi ORDER BY orginal_in_draught DESC ) A

Mysql按欄位分組最大值記錄

要求:獲得按table1_id分組,並且age最大的記錄資訊,即2、3、5條方法一:select * from (select * from table2 order by age desc) as a group by a.table1_id方法二:select a.* f

mysql查詢某屬性下所有值的N記錄

例如, 姓名 科目 成績 王 數學 100 王 語文 99 王 英語 98 王 生物 97 王 政治 96 王 物理 95 王 化學 94 王 體育 93

用postgresql特性簡化group by 後每組n記錄的方法

需求: 求一段時間內各生產線前3名壞機的原因及壞機數. step 1, 從將原始資料中抽取品質資料放於t96臨時表 create temp table t96 on commit drop as

Mysql獲取每組N記錄(開窗函式)

由於專案需求,我需要把查詢出來的資料進行分組,並且只需要每組前三條記錄,後來瞭解到MySQL實現開窗函式的方法,用了之後發現是可以,但今天發現這個方法在Navicat Premium軟體中執行第一次所查詢出來的資料不符合,執行第二次才是符合要求,具體原因有空再瞭

Oracle 子查詢,按降序排列,n

比如,有一個student 表: id             student_name      1                                  s1 2                                  s2 03       

MYSQL 查詢N記錄某個欄位的和

假設有個課程表course,其中有個欄位Ccredit,現在想查詢這個欄位前N條記錄的和,那麼這個語句該怎麼寫? 可能首先想到的就是:select sum(Ccredit) from course limit N; 可是經過試驗卻發現查詢出來的是這個欄位所有記錄的和,而不是前

Mysql獲取每組N記錄

Select基礎知識 我們在實現select語句的時候,通用的sql格式如下: select *columns* from *tables* where *predicae1* group by *columns* having

從排序後的結果集中刪除 n記錄

not null ima -- date com alt delet lec log 端午有人休息,有人忙 操作前數據: --從排序後的結果集中刪除 前n條記錄delete from emp where empno in (select empno

SQL Server查詢N記錄的三種方法

SQL Server查詢前N條記錄是我們經常要用到的操作,下面對SQL Server查詢前N條記錄的方法作了詳細的介紹,如果您感興趣的話,不妨一看。 SQL Server查詢前N條記錄: 因為id可能不是連續的,所以不能用取得10<id<20的記錄的方法。 有三種方法可以實現: 一、