0
点赞
收藏
分享

微信扫一扫

mysql子查询题型解析(2)



文章目录

  • ​​子查询(2)​​
  • ​​(1)带比较运算符的子查询:(子查询的返回值只有一个)​​
  • ​​(2)带ANY关键字的子查询​​
  • ​​(3)带all关键字的子查询​​
  • ​​(4)子查询嵌套在select语句的from子句中;​​
  • ​​题型综合:(左右外连接)(难)​​

子查询(2)

(1)带比较运算符的子查询:(子查询的返回值只有一个)

1、子查询可以使用其他的比较运算符,如”<”,”>=”,”=”,”!=”,”>”等;

例1:查询和于田田一个系的学生的名字;(student)

select sname from student where sdept in
(select sdept from student where sname="于田田");

in和=相比,in是万能的

例2:查询a03课程成绩比2007010104这位同学a03成绩高的学生的学号以及对应的成绩;

select sno,degree from sc where cno="a03" 
and
degree>(select degree from sc where sno="2007010104" and cno="a03");

例3:查询a03课程成绩不高于2007010104这位同学a03成绩的学生的学号以及对应的成绩;

select sno,degree from sc where cno="a03" 
and
degree<=(select degree from sc where sno="2007010104" and cno="a03" );

(2)带ANY关键字的子查询

ANY(任意的一个)关键字表示满足其中任意一个条件即可

语法格式:select 字段 from表名 where字段>(比较运算符)any(子查询);

例1:查询其他系出生日期有大于信息工程系任意一个学生出生日期的学生的出生日期;

(1)

select sbirthday from student 
where sbirthday>
any(select sbirthday from student where sdept="信息工程系") and sdept!="信息工程系";

(2)

select sbirthday from student 
where sbirthday>(select min(sbirthday) from student where sdept="信息工程系");

例2:查询成绩有高于学号为2007010104这位同学的任意一门成绩的学生的学号、课程号和成绩;

(1)

select sno,cno,degree from sc 
where degree>any(select degree from sc where sno="2007010104");

(2)

select sno,cno,degree from sc 
where degree>(select min(degree) from sc where sno="2007010104");

例3:查询成绩有和学号为2007010104这位同学的任意一门成绩的相等学生的学号、课程号和成绩;

select sno,cno,degree from sc 
where degree=any(select degree from sc where sno="2007010104");

(3)带all关键字的子查询

all(全部)

语法格式:select 字段 from表名 where字段>(比较运算符)all(子查询);

例1:查询比信息工程所有学生出生日期都要小的学生的出生日期;

(1)

select sbirthday from student 
where sbirtdhay<all(select sbirthday from student where sdept="信息工程");

(2)

select sbirthday from student 
where sbirtdhay<(select min(sbirtdhay) from student where sdept="信息工程");

例2:查询成绩低于学号为2007010104这位同学的所有课程成绩的学生的学号,所选课程,成绩;

(1)

select sno,cno,degree from sc 
where degree<all(select degree from sc where sno="2007010104");

(4)子查询嵌套在select语句的from子句中;

语法格式如下:select字段from(子查询)as 表的别名;

例1:查询豆豆所在的系别;

select sdept from student where sname="豆豆";
select sdept from (select * from student where sname="豆豆");

例2:查询选修了a01这门课程,并且平均成绩在80分以上的学生的名字;

第一种写法:

Select sname from student 
where sno in(select sno,avg(degree) from sc
where sno in(select sno from sc where cno=”a01”)
group by sno
having avg(degree)>80);

第二种写法:

Select sname from student 
where sno in(selectsno from(select sno,avg(degree) from sc
where sno in(select sno from sc where cno=”a01”)
group by sno
having avg(degree)>80));

第三种写法:

SELECT sname FROM student 
WHERE sno IN(SELECT sno FROM
(SELECT sno,AVG(degree) FROM sc
WHERE sno IN(SELECT sno FROM sc WHERE cno="a01")
GROUP BY sno
HAVING AVG(degree)>80)a);

判断哪一种写法是正确的?

(1)带in关键字的子查询

(2)子查询出现在from 子句当中

所以(3)是正确的

Every derived table must have its own alias:每一个派生出来的表都必须有一个自己的别名,那我给派生表加上别名即可

与题型有关表的标签如下:

mysql子查询题型解析(2)_信息工程

mysql子查询题型解析(2)_子查询_02mysql子查询题型解析(2)_数据库_03

mysql子查询题型解析(2)_mysql_04

mysql子查询题型解析(2)_mysql_05

student(学号、姓名、性别、课程、系别、课程号等)

teacher(工号、姓名、性别、系别等)

teaching(课程号 工号等)

course(课程号 课程名字)

sc(学号、课程号、成绩)

题型综合:(左右外连接)(难)

若答案有误或者存在更好的写法,欢迎留言。

(1)查询选修了李先生所授课程的学生姓名;(涉及的表teacher,teaching,sc,

student)

select sname from student where sno in
(select sno from sc where cno in
(select cno from teaching where tno in
(select tno from teacher where tname="李先生")));

(2)查询选修的有a01课程,并且选修课程数少于4门的学生的学号以及它所选修的课程数;(所涉及的表sc表)

select sno,count(cno) from sc where sno in
(select sno from sc where cno="a01")
group by sno
having count(cno)<4;

(3)查询选修的有a01课程,并且选修课程数少于4门的学生的学号以及它所选修的课程有哪些;(所涉及的表sc表)

select sno,cno from sc where sno in
(select sno from
(select sno,count(cno) from sc where sno in
(select sno from sc where cno="a01")
group by sno
having count(cno)<4)as a);

(4)查询选修的有a01课程,并且选修课程数少于4门的学生的名字;(所涉及的表sc,student表)

select sname from student where sno in
(select sno from
(select sno,count(cno) from sc where sno in
(select sno from sc where cno="a01")
group by sno
having count(cno)<4)as a);

(5)查询和小杨在同一个班的学生的平均成绩;(所涉及的表student,sc)

select avg(degree) from sc where sno in
(select sno from student where classno from student where sname="小杨"));

(6)查询和小杨在同一个班,并且平均成绩高于80分的学生名字;(所涉及的表student,sc表)

select sname from student where sname!="小杨" and sno in
(select sno from(select sno,avg(degree) from sc where sno in
(select sno from student where classno in
(select classno from student where sname="小杨"))
group by sno
having avg(degree)>80)as a);

(7)查询成绩有和学号为2007010101这位同学的任意一门成绩的相等学生的学号、课程号和成绩;(所涉及的表sc表)

select sno,cno,degree from sc where degree=any
(select degree from sc where sno="2007010101" and sno!="2007010101");



举报

相关推荐

0 条评论