2021-10-25
连接查询包括等值查询,非等值查询,自然查询(是有损连接),还有自身连接,外连接(包括左外连接和右外连接)以及复合条件连接。
–1、列出所有课程被选修的详细情况,包括课程号、课程名、学号、姓名及成绩
select b.cno,cname,a.sno,sname,grade
from student a,sc b,course c
where a.sno=b.sno and c.cno=b.cno
–2、查询名字中第2个字为‘阳’的学生姓名和学号及选修的课程号、课程名
select sname,a.sno,b.cno,cname
from student a,sc b,course c
where a.sno=b.sno and c.cno=b.cno and a.sname like ‘_阳%’
–3、列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩
select a.sno,sname,sdept,b.cno,grade
from student a,sc b,course c
where a.sno=b.sno and c.cno=b.cno and (c.cname='数学’or c.cname=‘大学英语’)
–4、查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息
select b.*
from student a,student b
where a.sname=‘张立’ and a.sage!=b.sage
–5、查询选修了以‘DB_’开头,且倒数第3个字符为‘s’的课程的学生的详细信息
select a.*
from student a,sc b,course c
where a.sno=b.sno and c.cno=b.cno and cname like’DB_%s__’ ESCAPE ‘’
连接查询可以针对一个查询同时涉及两个及以上的表,这样可以解决多种的查询问题。