连接查询
若在一个查询请求中,涉及到多个表中的数据,则需要进行连接查询。 按连接方式分: 内连接 (默认连接类型)INNER JOIN、外连接、自连接、交叉连接
1.等值连接—连接运算符为“=”
例:查询每个学生的基本信息及其选课情况。
SELECT * FROM student INNER JOIN sc ON stuent.sno=sc.sno; 或: SELECT * FROM student, sc WHERE student.sno=sc.sno;
2.自然连接–将等值连接中重复的属性列去掉
例:查询每个学生的基本信息及其选课情况。
SELECT student.sno, sname, ssex, sage, sdept, cno, grade FROM student INNER JOIN sc ON student.sno=sc.sno;
3.自连接:一个表进行自连接
需要给表起别名以示区别;由于所有属性名都是同名属性,因此必须使用别名前缀。
例:查询各门课程的先修课情况
SELECT FIRST.cno,FIRST.cname, SECOND.cname AS '先修课' FROM course FIRST,course SECOND WHERE FIRST.cpno=SECOND.cno;
对于多表连接,若连接方式为内连接,则多采用将连接条件写在where子句中的形式,更加简洁。 如: SELECT sname,cname,grade FROM student,sc,course WHERE student.sno=sc.sno AND sc.cno=course.cno AND sdept=‘计算机系’
对于多表连接,若多个表中均有同名列,则必须在列名指定输出列来自与哪个表。 如: SELECT student.sno,sname, cname,grade FROM student,sc,course WHERE student.sno=sc.sno AND sc.cno=course.cno AND sdept=‘计算机系’
外连接与内连接的区别:
内连接操作只输出满足连接条件的元组。
外连接操作以指定表为连接主体,将主体表中不满足连接条件的元组一并输出。
1.左外连接:LEFT [OUTER] JOIN
例:查询学生的选课情况,要求输出学号,姓名,课程代号,成绩。(包括学生未选课的信息) SELECT student.sno,sname,cno,grade FROM student LEFT JOIN sc ON student.sno=sc.sno ORDER BY student.sno;
2.右外连接:RIGHT [OUTER] JOIN
例: 查询教师的工资情况 SELECT gc.*,js.xm FROM gz RIGHT JOIN js ON gz.gh=js.gh ORDER BY xm;
3.完全连接:FULL [OUTER] JOIN
例: 查询教师的工资情况 SELECT gz.*, js.* FROM gz FULL JOIN js ON gz.gh=js.gh; #完全连接返回两表中的所有行,其中匹配记录仅显示一次;非匹配行的数据不可用的列显示NULL值。
一个表中的每行与另一个表中的每行连接; 结果集中行的数量是第一个表中行的数量与第二个表中行的数量的乘积。
例: SELECT titles.*,publishers.* FROM titles CROSS JOIN publishers;
下面开始做题。
SELECT course.cno,cname,COUNT(grade),MAX(grade),Min(grade),ROUND(AVG(grade)) FROM course,sc WHERE course.cno=sc.cno group by course.cno ORDER BY course.cno;
select student.sno,sname,cname,grade from student,sc,course where grade is not null and student.sno=sc.sno and course.cno=sc.cno order by sno asc,cname desc;
select student.sno,sname,grade from student,sc,course where (grade is not null) and cname='高等数学' and student.sno=sc.sno and course.cno=sc.cno ;
select student.sno,sname,avg(grade),count(*) from student,sc where student.sno=sc.sno group by student.sno order by avg(grade) desc;
SELECT sc.sno,sname,ROUND(AVG(grade)) FROM sc,student,course WHERE student.sno=sc.sno and course.cno=sc.cno group by sc.sno ORDER BY avg(grade)desc limit 1,4;
select student.sno,sname,course.cname,grade from student,sc,course where sname like '%海%' and student.sno=sc.sno and course.cno=sc.cno;
select sc.sno,cno,grade,avggrade from sc, (select sno,round(AVG(grade))as avggrade from sc group by sno)AS AVG_sc where sc.sno=AVG_sc.sno and sc.grade>AVG_sc.avggrade order by sc.sno;
select student.sno,sname,avg(grade),count(*) from student,sc where student.sno=sc.sno group by student.sno order by avg(grade) desc;
select student.sno,sname,avg(grade) from student,sc where student.sno=sc.sno group by sc.sno having avg(grade)>70 order by avg(grade)desc;
select a.cno,a.cname,b.cname from course a,course b where a.cpno=b.cno;
select sno,sname,jg from student,zy where zy.zydh=student.zydh and zymc='网络工程' order by sno desc;
select s.sno,s.sname,cno,grade from student s left join sc on (s.sno=sc.sno) where s.sno like '95%';
select xmc,zymc,count(*) from student,zy,xb where student.zydh=zy.zydh and student.xdh=xb.xdh group by zy.zydh order by xmc;
select student.sno,sname,avg(grade) from student,sc where sdept='计算机系' and student.sno=sc.sno group by student.sno having avg(grade)>70 order by avg(grade) desc ;
select sno,sname from student where sno in( select sc.sno from sc group by sc.sno having count(*) is not null) order by sno;
select course.cno,cname,count(*) from sc,course where (grade<60 or grade is null) and course.cno=sc.cno group by cno;
SELECT student.sno,sname,COUNT(*) FROM sc,student WHERE grade<60 and student.sno=sc.sno GROUP BY sno HAVING COUNT(*)>=2 ORDER BY COUNT(*) DESC, sno asc;
select student.sno,sname,avg(grade) from student,sc,course where student.sno=sc.sno and course.cno=sc.cno and sdept='计算机系' group by student.sno order by avg(grade) desc limit 3;
select student.sno,sname,cname,sdept,grade from student,sc,course where cname='高等数学' and student.sno=sc.sno and course.cno=sc.cno order by grade desc limit 3;
select student.sno,sname,course.cname,grade from student,sc,course where sdept='计算机系' and student.sno=sc.sno and course.cno=sc.cno order by sno;