MySQL入门第七课:外连接,自连接(重点,附加习题和答案)请认真学习

L17 外连接和子连接
     语法格式:
    from 表1 left [outer] join 表2 on ....

    有剩余记录的表放在左表 
 
    左外连接(在左表中有,右表中没有的记录)
    1.查询没有选修课程的学生的基本信息
    select stuinfo.*from stuinfo left join stumarks on stuinfo.stuno =     stumarks.stuno where stumarks.stuno is null;
    
    2.查询没有学生选的课程的基本信息(左表是有剩余记录的表)
    select stucourse.*from stucourse left join stumarks on stucourse.cno =     stumarks.cno join stuinfo on stumarks.stuno = stuinfo.stuno where     stumarks.stuno is null;

    3.查询没有选修0003号课程的学生的学号和姓名(拔高)
    select stuinfo.stuno,stuname from stuinfo left join stumarks on     stumarks.stuno = stuinfo.stuno and cno='0003' where stumarks.stuno is null;
    
    自连接:
    特殊在两个表是完全相同的,可以看成是一张表的两个副本的连接,为区别副本,要起别名
    
    1.查询入职日期早于其上级领导入职日期的员工的信息
    select *from emp e1 ,emp e2
    where e1.mgr = e2.empno and e1.hiredate < e2.hiredate;
    
    2.查询员工的姓名和其上级姓名
    select e1.ename 员工姓名,e2.ename 上级姓名 from emp e1,emp e2
    where e1.mgr = e2.empno ;
    
    3.查找同一课程成绩相同的选课记录
    select s1.stuno,s2.stuno,s1.stuscore from stumarks s1,stumarks s2
        where s1.stuscore = s2.stuscore and s1.stuno != s2.stuno and s1.cno =     s2.cno;

161页 5,6(左外连接) 1,2,3,4,7(内连接)  156页 

select stuinfo.*from stuinfo,stumarks
where stuinfo.stuno = stumarks.stuno and cno='0001' and stuscore<60;


L18 自连接
(1) 查询选修了课程的学生信息
    select *from stuinfo
    where stuno in(select distinct stuno from stumarks);
(2)查询没有选修课程的学生信息
    select *from stuinfo
    where stuno not in(select distinct stuno from stumarks);
(3)查询成绩最高的选课记录
    select *from stumarks
    where stuscore = (select max(stuscore) from stumarks);
(4)查询成绩比所有成绩的平均成绩高的选课记录
    select *from stumarks
    where stuscore > (select avg(stuscore) from stumarks);
(5)查询成绩比该课程的平均成绩高的记录(相关子查询)
    select *from stumarks m1
    where stuscore > (select avg(stuscore) from stumarks m2 where m1.cno=m2.cno);
(6)查询选修了高等数学这门课的学生的基本信息
    select *from stuinfo 
    where stuno in(select distinct stuno from stumarks where cno = (select cno from stucourse     where cname="高等数学")); 
     


课后练习
(2)查询选修“高等数学”课程且成绩在 80-90 分之间的学生学号及成绩。
select stuno,stuscore from stumarks
where cno = (select cno from stucourse where cname="高等数学") and stuscore between 80 and 90;

(3)查询选修“数据结构”课程的学生的学号,姓名和性别。
select stuno,stuname,stusex from stuinfo
where stuno in(select stuno from stumarks where cno = (select cno from stucourse where cname = "数据结构"));

(4)查询至少选修一门课程的女学生的学号及姓名。
select stuno,stuname from stuinfo
where stuno in(select distinct stuno from stumarks) and stusex="女";

(5)查询没有选修“0003”这门课的学生的学号及姓名。
select stuno,stuname from stuinfo
where stuno not in(select stuno from stumarks where cno = "0003");

(6)查询没有学生选修的课程号及课程名称。
select cno,cname from stucourse
where cno not in(select cno from stumarks);

(7)查询“0001”号课程不及格的学生信息
 select *from stuinfo
 where stuno in(select stuno from stumarks where stuscore<60 and cno="0001");

(8)查询年龄小于所有女生的男生的学号、姓名及出生日期。
第一种写法:
 select stuno,stuname,stubirthday from stuinfo
 where stusex="男" and stubirthday > all(select stubirthday from stuinfo where stusex="女");

第二种写法:
 select stuno,stuname,stubirthday from stuinfo
 where stuno in(select stuno from stuinfo where stusex="男" and stubirthday >     all(select stubirthday from stuinfo where stusex ="女"));

你可能感兴趣的:(Mysql语法专栏,mysql,数据库)