龙空技术网

MySQL 查询练习

雪地大懒猫 187

前言:

此刻兄弟们对“mysql查询排除”大约比较看重,小伙伴们都需要知道一些“mysql查询排除”的相关资讯。那么小编在网摘上收集了一些关于“mysql查询排除””的相关内容,希望大家能喜欢,姐妹们快快来了解一下吧!

--建表

--学生表

CREATE TABLE `bc_student`(

`stud_id` VARCHAR(20),

`stud_name` VARCHAR(20) NOT NULL DEFAULT '',

`stud_birth` VARCHAR(20) NOT NULL DEFAULT '',

`stud_sex` VARCHAR(10) NOT NULL DEFAULT '',

PRIMARY KEY(`stud_id`)

);

--课程表

CREATE TABLE `bc_course`(

`course_id` VARCHAR(20),

`course_name` VARCHAR(20) NOT NULL DEFAULT '',

`t_id` VARCHAR(20) NOT NULL,

PRIMARY KEY(`course_id`)

);

--教师表

CREATE TABLE `bc_teacher`(

`t_id` VARCHAR(20),

`t_name` VARCHAR(20) NOT NULL DEFAULT '',

PRIMARY KEY(`t_id`)

);

--成绩表

CREATE TABLE `bc_score`(

`stud_id` VARCHAR(20),

`course_id` VARCHAR(20),

`teacher_score` INT(3),

PRIMARY KEY(`stud_id`,`course_id`)

);

--插入学生表测试数据

insert into bc_student values('01' , '赵一' , '1990-01-01' , '男');

insert into bc_student values('02' , '钱二' , '1990-12-21' , '男');

insert into bc_student values('03' , '孙三' , '1990-05-20' , '男');

insert into bc_student values('04' , '李四' , '1990-08-06' , '男');

insert into bc_student values('05' , '周五' , '1991-12-01' , '女');

insert into bc_student values('06' , '吴六' , '1992-03-01' , '女');

insert into bc_student values('07' , '郑七' , '1989-07-01' , '女');

insert into bc_student values('08' , '王八' , '1990-01-20' , '女');

--课程表测试数据

insert into bc_course values('01' , '语文' , '02');

insert into bc_course values('02' , '数学' , '01');

insert into bc_course values('03' , '英语' , '03');

--教师表测试数据

insert into bc_teacher values('01' , '张三丰');

insert into bc_teacher values('02' , '李乾坤');

insert into bc_teacher values('03' , '王大牙');

--成绩表测试数据

insert into bc_score values('01' , '01' , 80);

insert into bc_score values('01' , '02' , 90);

insert into bc_score values('01' , '03' , 99);

insert into bc_score values('02' , '01' , 70);

insert into bc_score values('02' , '02' , 60);

insert into bc_score values('02' , '03' , 80);

insert into bc_score values('03' , '01' , 80);

insert into bc_score values('03' , '02' , 80);

insert into bc_score values('03' , '03' , 80);

insert into bc_score values('04' , '01' , 50);

insert into bc_score values('04' , '02' , 30);

insert into bc_score values('04' , '03' , 20);

insert into bc_score values('05' , '01' , 76);

insert into bc_score values('05' , '02' , 87);

insert into bc_score values('06' , '01' , 31);

insert into bc_score values('06' , '03' , 34);

insert into bc_score values('07' , '02' , 89);

insert into bc_score values('07' , '03' , 98);

-- 1、查询"语文"课程比"数学"课程成绩高的学生的信息及课程分数

select a.* ,b.stud_score 01_score,c.stud_score 02_score from bc_student a

LEFT JOIN bc_score b ON a.stud_id = b.stud_id and b.course_id = '01'

LEFT JOIN bc_score c ON a.stud_id = c.stud_id and c.course_id = '02'

where c.stud_score > b.stud_score;

-- 或者写成

select a.* ,b.stud_score 01_score,c.stud_score 02_score from bc_student a , bc_score b, bc_score c

where b.course_id = '01'

and c.course_id = '02'

and a.stud_id = b.stud_id

and a.stud_id =c.stud_id

and c.stud_score > b.stud_score;

-- 2、查询"语文"课程比"数学"课程成绩低的学生的信息及课程分数

select a.* ,b.stud_score 01_score,c.stud_score 02_score from bc_student a

LEFT JOIN bc_score b ON a.stud_id = b.stud_id and b.course_id = '01'

LEFT JOIN bc_score c ON a.stud_id = c.stud_id and c.course_id = '02'

where c.stud_score < b.stud_score;

-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT

b.stud_id,

b.stud_name,

ROUND( AVG( a.stud_score ), 2 ) AS avg_score

FROM

bc_student b

JOIN bc_score a ON b.stud_id = a.stud_id

GROUP BY

b.stud_id,

b.stud_name

HAVING

avg_score >= 60;

-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

-- (包括有成绩的和无成绩的)

SELECT

b.stud_id,

b.stud_name,

ROUND( AVG( a.stud_score ), 2 ) AS avg_score

FROM

bc_student b

JOIN bc_score a ON b.stud_id = a.stud_id

GROUP BY

b.stud_id,

b.stud_name

HAVING

avg_score < 60

union

SELECT

b.stud_id,

b.stud_name,

0 AS avg_score

FROM

bc_student b

where b.stud_id not in (

select stud_id from bc_score

);

-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

SELECT

a.stud_id,

a.stud_name,

count( b.course_id ) AS sum_course,

IFNULL(sum( b.stud_score ),0) AS sum_score

FROM

bc_student a

LEFT JOIN bc_score b ON a.stud_id = b.stud_id

GROUP BY

a.stud_id,

a.stud_name;

-- 6、查询"李"姓老师的数量

select count(1) from bc_teacher where t_name like '李%';

-- 7、查询学过"张三丰"老师授课的同学的信息

SELECT a.* FROM bc_student a

JOIN bc_score b ON a.stud_id = b.stud_id

WHERE b.course_id IN (

SELECT course_id FROM bc_course WHERE t_id =(

SELECT t_id FROM bc_teacher WHERE t_name = '张三丰'

));

-- 8、查询没学过"张三丰"老师授课的同学的信息

select c.* from bc_student c where c.stud_id not in(

select a.stud_id from bc_student a join bc_score b on a.stud_id=b.stud_id where b.course_id in(

select a.course_id from bc_course a join bc_teacher b on a.t_id = b.t_id where t_name ='张三丰'));

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select a.* from bc_student a, bc_score b, bc_score c

where a.stud_id = b.stud_id and a.stud_id = c.stud_id

and b.course_id = '01' and c.course_id = '02';

-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select a.* from bc_student a

where a.stud_id in (select stud_id from bc_score where course_id='01' )

and a.stud_id not in(select stud_id from bc_score where course_id='02');

-- 或者写成

select a.* from bc_student a

where EXISTS (select 1 from bc_score b where a.stud_id = b.stud_id and b.course_id='01' )

and not EXISTS (select 1 from bc_score c where a.stud_id = c.stud_id and c.course_id='02');

-- 11、查询没有学全所有课程的同学的信息

select s.* from bc_student s

left join bc_score s1 on s1.stud_id = s.stud_id

group by s.stud_id having count(s1.course_id )< (select count(1) from bc_course);

select * from bc_student where stud_id not in (

select stud_id from bc_score t1

group by stud_id having count(1) =(select count(distinct course_id) from bc_course));

-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

select * from bc_student where stud_id in(

select a.stud_id from bc_score a where a.course_id in (select a.course_id from bc_score a where a.stud_id='01')

);

-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

SELECT

*

FROM

bc_student

WHERE

stud_id IN (SELECT stud_id FROM bc_score GROUP BY stud_id HAVING COUNT(stud_id) = (

#下面的语句是找到'01'同学学习的课程数

SELECT COUNT(course_id) FROM bc_score WHERE stud_id = '01'

)

)

AND stud_id NOT IN (

#下面的语句是找到学过‘01’同学没学过的课程,有哪些同学。并排除他们

SELECT stud_id FROM bc_score

WHERE course_id IN(

#下面的语句是找到‘01’同学没学过的课程

SELECT DISTINCT course_id FROM bc_score

WHERE course_id NOT IN (

#下面的语句是找出‘01’同学学习的课程

SELECT course_id FROM bc_score WHERE stud_id = '01'

)

) GROUP BY stud_id

) #下面的条件是排除01同学

AND stud_id NOT IN ('01');

-- 或者

SELECT

t3.*

FROM

(

SELECT

stud_id,

group_concat(course_id ORDER BY course_id) group1

FROM

bc_score

WHERE

stud_id != '01'

GROUP BY

stud_id

) t1

INNER JOIN (

SELECT

group_concat(course_id ORDER BY course_id) group2

FROM

bc_score

WHERE

stud_id = '01'

GROUP BY

stud_id

) t2 ON t1.group1 = t2.group2

INNER JOIN bc_student t3 ON t1.stud_id = t3.stud_id;

-- 14、查询没学过"张三丰"老师讲授的任一门课程的学生姓名

select a.stud_name from bc_student a

where a.stud_id not in (

select stud_id from bc_score where course_id in (

select course_id from bc_course where t_id =

(select t_id from bc_teacher where t_name = '张三丰')));

-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select a.stud_id,a.stud_name,ROUND(AVG(b.stud_score)) from bc_student a

left join bc_score b on a.stud_id = b.stud_id where a.stud_id in(

select stud_id from bc_score where stud_score < 60 GROUP BY stud_id having count(1) >= 2 )

GROUP BY a.stud_id,a.stud_name;

-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息

select a.*,b.course_id,b.stud_score from bc_student a,bc_score b

where a.stud_id = b.stud_id and b.course_id='01' and b.stud_score < 60

ORDER BY b.stud_score DESC;

-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select a.stud_id,(select stud_score from bc_score where stud_id = a.stud_id and course_id='01') as 语文,

(select stud_score from bc_score where stud_id = a.stud_id and course_id='02') as 数学,

(select stud_score from bc_score where stud_id = a.stud_id and course_id='03') as 英语,

round(avg(stud_score),2) as 平均分 from bc_score a GROUP BY a.stud_id ORDER BY 平均分 DESC;

SELECT a.stud_id,MAX(CASE a.course_id WHEN '01' THEN a.stud_score END ) 语文,

MAX(CASE a.course_id WHEN '02' THEN a.stud_score END ) 数学,

MAX(CASE a.course_id WHEN '03' THEN a.stud_score END ) 英语,

avg(a.stud_score),b.stud_name FROM bc_score a

JOIN bc_student b ON a.stud_id=b.stud_id GROUP BY a.stud_id ORDER BY 5 DESC ;

-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

select a.course_id,b.course_name,MAX(stud_score),MIN(stud_score),ROUND(AVG(stud_score),2),

ROUND(100*(SUM(case when a.stud_score >= 60 then 1 else 0 end)/SUM(case when a.stud_score then 1 else 0 end)),2) as 及格率,

ROUND(100*(SUM(case when a.stud_score >= 70 and a.stud_score <= 80 then 1 else 0 end)/SUM(case when a.stud_score then 1 else 0 end)),2) as 中等率,

ROUND(100*(SUM(case when a.stud_score >= 80 and a.stud_score <= 90 then 1 else 0 end)/SUM(case when a.stud_score then 1 else 0 end)),2) as 优良率,

ROUND(100*(SUM(case when a.stud_score >= 90 then 1 else 0 end)/SUM(case when a.stud_score then 1 else 0 end)),2) as 优秀率

from bc_score a left join bc_course b on a.course_id = b.course_id GROUP BY a.course_id,b.course_name

-- 19、按各科成绩进行排序,并显示排名

-- mysql没有rank函数

select a.stud_id,a.course_id,

@i:=@i +1 as i保留排名,

@k:=(case when @score=a.stud_score then @k else @i end) as rank不保留排名,

@score:=a.stud_score as score

from (

select stud_id,course_id,stud_score from bc_score GROUP BY stud_id,course_id,stud_score ORDER BY stud_score DESC

)a,(select @k:=0,@i:=0,@score:=0)s

-- 20、查询学生的总成绩并进行排名

select a.stud_id,

@i:=@i+1 as i,

@k:=(case when @score=a.sum_score then @k else @i end) as rank1,

@score:=a.sum_score as score

from (select stud_id,SUM(stud_score) as sum_score from bc_score GROUP BY stud_id ORDER BY sum_score DESC)a,

(select @k:=0,@i:=0,@score:=0)s

-- 21、查询不同老师所教不同课程平均分从高到低显示

select a.t_id,c.t_name,a.course_id,ROUND(avg(stud_score),2) as avg_score

from bc_course a

left join bc_score b on a.course_id = b.course_id

left join bc_teacher c on a.t_id = c.t_id

GROUP BY a.course_id,a.t_id,c.t_name ORDER BY avg_score DESC;

-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

select d.*,c.排名,c.stud_score,c.course_id from (

select a.stud_id,a.stud_score,a.course_id,@i:=@i+1 as 排名 from bc_score a,(select @i:=0)s where a.course_id='01'

ORDER BY a.stud_score DESC

)c

left join bc_student d on c.stud_id=d.stud_id

where 排名 BETWEEN 2 AND 3

UNION

select d.*,c.排名,c.stud_score,c.course_id from (

select a.stud_id,a.stud_score,a.course_id,@j:=@j+1 as 排名 from bc_score a,(select @j:=0)s where a.course_id='02'

ORDER BY a.stud_score DESC

)c

left join bc_student d on c.stud_id=d.stud_id

where 排名 BETWEEN 2 AND 3

UNION

select d.*,c.排名,c.stud_score,c.course_id from (

select a.stud_id,a.stud_score,a.course_id,@k:=@k+1 as 排名 from bc_score a,(select @k:=0)s where a.course_id='03'

ORDER BY a.stud_score DESC

)c

left join bc_student d on c.stud_id=d.stud_id

where 排名 BETWEEN 2 AND 3;

-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

select distinct f.course_name,a.course_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比

from bc_score a

left join (

select course_id, SUM(case when stud_score >85 and stud_score <=100 then 1 else 0 end) as `85-100`,

ROUND(100*(SUM(case when stud_score >85 and stud_score <=100 then 1 else 0 end)/count(*)),2) as 百分比

from bc_score GROUP BY course_id

) b on a.course_id = b.course_id

left join (

select course_id,SUM(case when stud_score >70 and stud_score <=85 then 1 else 0 end) as `70-85`,

ROUND(100*(SUM(case when stud_score >70 and stud_score <=85 then 1 else 0 end)/count(*)),2) as 百分比

from bc_score GROUP BY course_id

) c on a.course_id = c.course_id

left join (

select course_id,SUM(case when stud_score >60 and stud_score <=70 then 1 else 0 end) as `60-70`,

ROUND(100*(SUM(case when stud_score >60 and stud_score <=70 then 1 else 0 end)/count(*)),2) as 百分比

from bc_score GROUP BY course_id

) d on a.course_id = d.course_id

left join (

select course_id,SUM(case when stud_score >=0 and stud_score <=60 then 1 else 0 end) as `0-60`,

ROUND(100*(SUM(case when stud_score >=0 and stud_score <=60 then 1 else 0 end)/count(*)),2) as 百分比

from bc_score GROUP BY course_id

) e on a.course_id = e.course_id

left join bc_course f on a.course_id = f.course_id;

-- 24、查询学生平均成绩及其名次

select a.stud_id,

@i:=@i+1 as '不保留空缺排名',

@k:=(case when @avg_score=a.avg_s then @k else @i end) as '保留空缺排名',

@avg_score:=avg_s as '平均分'

from (select stud_id,ROUND(AVG(stud_score),2) as avg_s from bc_score GROUP BY stud_id ORDER BY avg_s DESC)a,

(select @avg_score:=0,@i:=0,@k:=0)b;

-- 25、查询各科成绩前三名的记录

-- 1.选出b表比a表成绩大的所有组

-- 2.选出比当前id成绩大的 小于三个的

select a.stud_id,a.course_id,a.stud_score

from bc_score a

left join bc_score b on a.course_id = b.course_id and a.stud_score < b.stud_score

group by a.stud_id,a.course_id,a.stud_score HAVING COUNT(b.stud_id) < 3

ORDER BY a.course_id,a.stud_score DESC;

-- 26、查询每门课程被选修的学生数 √

select a.course_id,count(a.stud_id) from bc_score a GROUP BY a.course_id;

-- 27、查询出只有两门课程的全部学生的学号和姓名 √

SELECT

a.stud_id,

a.stud_name

FROM

bc_student a where stud_id in (

select stud_id from bc_score GROUP BY stud_id HAVING COUNT(course_id)=2);

-- 28、查询男生、女生人数 √

select stud_sex,COUNT(1) as 人数 from bc_student GROUP BY stud_sex;

-- 29、查询名字中含有"三"字的学生信息 √

select * from bc_student where stud_name like '%三%';

-- 30、查询同名同性学生名单,并统计同名人数

select a.stud_name,a.stud_sex,count(1) from bc_student a

JOIN bc_student b on a.stud_id !=b.stud_id and a.stud_name = b.stud_name and a.stud_sex = b.stud_sex

GROUP BY a.stud_name,a.stud_sex;

-- 31、查询1990年出生的学生名单 √

select stud_name from bc_student where stud_birth like '1990%';

-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select course_id,ROUND(AVG(stud_score),2) as avgscore from bc_score GROUP BY course_id ORDER BY avgscore DESC,course_id ASC;

-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

select a.stud_id,b.stud_name,ROUND(avg(a.stud_score),2) as avgscore from bc_score a

left join bc_student b on a.stud_id = b.stud_id GROUP BY stud_id HAVING avgscore >= 85;

-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数

select a.stud_name,b.stud_score from bc_score b join bc_student a on a.stud_id = b.stud_id

where b.course_id = (select course_id from bc_course where course_name ='数学')

and b.stud_score < 60;

-- 35、查询所有学生的课程及分数情况;

select a.stud_id,a.stud_name,

SUM(case c.course_name when '语文' then b.stud_score else 0 end) as '语文',

SUM(case c.course_name when '数学' then b.stud_score else 0 end) as '数学',

SUM(case c.course_name when '英语' then b.stud_score else 0 end) as '英语',

SUM(b.stud_score) as '总分'

from bc_student a

left join bc_score b on a.stud_id = b.stud_id

left join bc_course c on b.course_id = c.course_id

GROUP BY a.stud_id,a.stud_name;

-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

select a.stud_name,b.course_name,c.stud_score

from bc_course b

left join bc_score c on b.course_id = c.course_id

left join bc_student a on a.stud_id = c.stud_id

where c.stud_score >= 70;

-- 37、查询不及格的课程

select a.stud_id,a.course_id,a.stud_score,b.course_name from bc_score a, bc_course b where a.course_id = b.course_id and a.stud_score < 60;

select a.stud_id,a.course_id,b.course_name,a.stud_score from bc_score a left join bc_course b on a.course_id = b.course_id where a.stud_score < 60;

-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;

select a.stud_id,b.stud_name from bc_score a LEFT JOIN bc_student b on a.stud_id = b.stud_id

where a.course_id = '01' and a.stud_score>=80;

-- 39、求每门课程的学生人数 √

select course_id,count(1) from bc_score GROUP BY course_id;

-- 40、查询选修"张三丰"老师所授课程的学生中,成绩最高的学生信息及其成绩

-- 查询老师id

select course_id from bc_course c,bc_teacher d where c.t_id=d.t_id and d.t_name='张三丰';

-- 查询最高分(可能有相同分数)

select MAX(stud_score) from bc_score where course_id = '02';

-- 查询信息

select a.*,b.stud_score,b.course_id,c.course_name from bc_student a

LEFT JOIN bc_score b on a.stud_id = b.stud_id

LEFT JOIN bc_course c on b.course_id=c.course_id

where b.course_id =(select course_id from bc_course c,bc_teacher d where c.t_id=d.t_id and d.t_name='张三丰')

and b.stud_score in (select MAX(stud_score) from bc_score where course_id = '02');

-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select DISTINCT b.stud_id,b.course_id,b.stud_score from bc_score a,bc_score b

where a.course_id != b.course_id and a.stud_score = b.stud_score;

-- 42、查询每门功成绩最好的前两名

select a.stud_id,a.course_id,a.stud_score from bc_score a

where (select COUNT(1) from bc_score b where b.course_id = a.course_id and b.stud_score >= a.stud_score) <= 2 ORDER BY a.course_id;

-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select course_id,count(*) as total from bc_score GROUP BY course_id HAVING total > 5 ORDER BY total,course_id ASC;

-- 44、检索至少选修两门课程的学生学号

select stud_id,count(*) as sel from bc_score GROUP BY stud_id HAVING sel>=2;

-- 45、查询选修了全部课程的学生信息

select * from bc_student where stud_id in(

select stud_id from bc_score GROUP BY stud_id HAVING count(1)=(select count(1) from bc_course) );

-- 46、查询各学生的年龄

-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

select stud_name,stud_birth,(DATE_FORMAT(NOW(),'%Y') - DATE_FORMAT(stud_birth,'%Y') -

(case when DATE_FORMAT(NOW(),'%m%d') < DATE_FORMAT(stud_birth,'%m%d') then 1 else 0 end)) as ages

from bc_student ;

select stud_name,stud_birth,(DATE_FORMAT(NOW(),'%Y') - DATE_FORMAT(stud_birth,'%Y') -

(case when DATE_FORMAT(NOW(),'%m%d') > DATE_FORMAT(stud_birth,'%m%d') then 0 else 1 end)) as ages

from bc_student ;

-- 47、查询本周过生日的学生

select * from bc_student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d')) =WEEK(stud_birth);

select * from bc_student where WEEKOFYEAR(stud_birth)=WEEKOFYEAR(DATE_FORMAT(NOW(),'%Y%m%d'));

-- 取当前日期是第几周

select WEEK(DATE_FORMAT(NOW(),'%Y%m%d'));

-- 48、查询下周过生日的学生

select * from bc_student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =WEEK(stud_birth);

-- 49、查询本月过生日的学生

select * from bc_student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d')) =MONTH(stud_birth);

-- 50、查询下月过生日的学生

select * from bc_student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =MONTH(stud_birth);

标签: #mysql查询排除