1. 程式人生 > 其它 >my sql 成績查詢面試題集錦

my sql 成績查詢面試題集錦

技術標籤:mysql

系統表格設定如下:
在這裡插入圖片描述

1.用一條SQL 語句 查詢出每門課都大於80 分的學生姓名

select a.s_id as s_id,a.s_name as s_name,b.c_id as c_id,b.scores as score  from test.student as a,test.score as b
where a.s_id=b.s_id
and a.s_id not in (
select distinct test.score.s_id as s_id from test.score
where test.score.scores<80
) 

顯示結果:

在這裡插入圖片描述

注:最後的括號後面加 “c”會報錯

其他思路:

A: select distinct name from table where name not in (select distinct name from table where fenshu<=80)
select name from table group by name having min(fenshu)>80

2.刪除重複的行,完全重複,包括主鍵
第一步 先建立一個複製表:

create table test.score_copy like  test.score

第二步 將要表格資訊去重後插入新表:

insert into
test.score_copy(s_id, c_id, scores) select * from test.score group by s_id,c_id,scores

第三步 刪除舊錶:

drop table test.score

第四步 將新表改名:

alter table test.score_copy rename test.score

通過以上操作就完成了

還有另一種情況是:刪除除了自動編號不同, 其他都相同的學生冗餘資訊

 delete tablename where
自動編號 not in(select min( 自動編號) from tablename group by學號, 姓名, 課程編號, 課程名稱, 分數)