mysql獲取某個班級學習成績的前幾名

2021-10-01 13:57:48 字數 2151 閱讀 8792

思路

1.首先根據班級分組獲取每個班級的所有學生並通過成績排序

2.過濾掉不符合條件的學生 最終獲取所有符合條件的學生

3.在通過學生唯一字段獲取學生資訊

需要用到mysql下面幾個函式

group_concat     可以把結果集中的多行中特定資料顯示成一列資料

find_in_set 可以檢視字串序列list(多個字串用逗號分隔)是否包含字串str    可以通過  between  and 來指定使用哪些字串             如find_in_set("ww","www,fff,kk")  between 2 and 3  等於  find_in_set("ww","fff,kk")   所以結果為false

substring_index   可以通過某個字元來分隔字串獲取前幾個子串

建立測試資料

drop table `student_score` ;

create table `student_score` (

`name` varchar(30) character set utf8 collate utf8_general_ci not null comment '姓名' ,

`class` varchar(30) character set utf8 collate utf8_general_ci not null comment '班級' ,

`score` int(10) not null comment '分數' ,

primary key (`class`, `name`)

);insert into student_score

values

('張三',"一年級",33),('張四',"一年級",44),('張五',"一年級",74),('張六',"一年級",80),('張七',"一年級",90),('張八',"一年級",100),

('李三',"二年級",33),('李四',"二年級",44),('李五',"二年級",74),('李六',"二年級",80),('李七',"二年級",90),('李八',"二年級",100),

('王三',"三年級",33),('王四',"三年級",44),('王五',"三年級",74),('王六',"三年級",80),

('趙三',"四年級",33),('趙四',"四年級",44),('趙五',"四年級",74),

('燕三',"五年級",33);

查詢出班級成績前三名的同學

#方式一

select s.* from student_score s,

(select substring_index(group_concat(name order by score desc),",",3) as names

from student_score s group by class

) as d

where find_in_set(s.name,d.names) between 1 and 3

order by class,score desc;

#方式二

select * from student_score where find_in_set(name,

(select group_concat(names) as names from

(select substring_index(group_concat(name order by score desc),",",3) as names

from student_score s group by class

) as d

)) order by class,score desc

查詢出班級成績第2名到第六名的同學

select s.* from student_score s,

(select substring_index(group_concat(name order by score desc),",",6) as names

from student_score s group by class

) as d

where find_in_set(s.name,d.names) between 2 and 6

order by class,score desc;

學習成績排名

create talbe score 姓名 varchar 14 科目 varchar 14 分數 int insert into score select 張三 數學 85 union all select 張三 語文 90 union all select 張三 英語 88 union all ...

爬蟲4 cookie登陸並爬取學習成績

最近專案太忙了,都沒空繼續學習爬蟲,前幾天實驗了各種姿勢帶cookie去登陸,都沒有成功,不明覺厲 依然沒有放棄,今天用同樣的辦法登陸以前大學的 居然成功了,我真是一臉懵逼 又看了一遍以前的學習成績,真是恨不得給自己兩耳屎,青春都讓狗吃了 哦,對了,以前還有一段學selenium的學習筆記,空了整理...

Mysql 獲取成績排序後的名次

其實就是輸出mysql的排序後的行號 rt 獲取單個使用者的成績在所有使用者成績中的排名 可以分兩步 1 查出所有使用者和他們的成績排名 select id,maxscore,rownum rownum 1 as rowno from t user,select rownum 0 b order b...