表連接查詢
一、交叉連接 - 笛卡爾積
查詢teacher和course表中所有的數據
select * from teacher,course;
select name,courseid,course.id,cname from teacher,course where teacher.courseid=course.id;
二、內連接
在關聯的兩張表中,把滿足條件的數據篩選出來
select 字段,... ...
from 表1
inner join 表2
on 條件
inner join 表3
on 條件
-- 使用內連接查詢teacher和course 表中的數據(姓名,年齡,課程名稱,課時) select t.name,t.age,c.cname,c.cduration from teacher as t inner join course as c on t.course_id=c.id; -- 查詢學員的姓名,年齡,所在班級名稱,專業名稱 並篩選出1902的學員 select s.name,s.age,c.classname,m.m_name from student as s inner join classinfo as c on s.class_id=c.id inner join major as m on s.major_id=m.id where c.classname='1902'; -- 查詢學員的姓名,畢業院校,所在班級,考試科目,考試成績 select s.name,s.school,c.classname,course.cname,sc.score from student as s inner join classinfo as c on s.class_id=c.id inner join score as sc on s.id=sc.stu_id inner join course on sc.course_id = course.id;
三、外鏈接
1.左外鏈接
作用:(1)左表中所有的數據都會查詢出來(即便不滿足條件)
(2)將右表中滿足關聯條件的數據查詢出來
(3)關鍵不上的數據關聯字段將以null作為填充(查詢哪些課程沒有老師去講 就用左外鏈)
語法:select 字
from A left join B
on 關聯條件
-- 左外鏈接,左表:course 右表:teacher -- 關聯條件:teacher.course_id=course.id -- 以左表為主 左表的id都顯示出來 查詢右表關聯信息,未關聯到的信息顯示為null,一般用來查詢null的值 select * from course left join teacher on teacher.course_id = course.id where teacher.id is null;
2.右外鏈接
作用:1.右表中所有的數據都會查詢出來(即便不滿足條件)
2.將左表中滿足關聯條件的數據查詢出來
3.關鍵不上的數據關聯字段將以null作為填充
語法:select 字段
from A right join B
on 關鍵條件
-- 右外鏈接,左表:teacher,右表:course, -- 關聯條件:teacher.course_id=course.id select * from teacher right join course on teacher.course_id = course.id where teacher.id is null;
-- 練習:查詢沒有參加考試的學生 select student.name,score.score from student left join score on student.id=score.stu_id where score.score is null;
四、子查詢
1.什么是子查詢:將一個查詢的結果作為外側操作的一個條件出現
2.語法:select ... from 表名 where 條件=(select ...);
-- 查詢student表中比‘李四’年齡大的學員的信息 select * from student where age>(select age from student where name='李四');

1 -- 練習:1、查詢考過‘王老師’老師所教課程的學校的信息 2 -- 方法1 3 select * from student where id in( 4 select stu_id from score where course_id=( 5 select course_id from teacher where name='王老師' 6 ) 7 ); 8 -- 方法2 9 select s.name,s.school 10 from student as s 11 inner join score as sc 12 on s.id =sc.stu_id 13 inner join teacher as t 14 on t.course_id=sc.course_id 15 where t.name='王老師'; 16 17 -- 練習:2、查詢在score表中有成績的學員的信息 18 select * from student where id in(select stu_id from score); 19 20 -- 練習:3、查詢'python基礎'課程並且分數在80分以上的學員的姓名和學校 21 -- 方法1 22 select student.name,student.school from 23 student left join score 24 on student.id = score.stu_id 25 left join course 26 on score.course_id = course.id 27 where score>80 and cname='python基礎'; 28 -- 方法2 29 select name,school from student where id in( 30 select stu_id from score where score>80 and course_id = ( 31 select id from course where cname='python基礎' 32 ) 33 ); 34 35 -- 練習:4、查詢和‘張三’相同班級以及相同專業的同學的信息 36 -- 方法1 37 select student.name,classinfo.classname,major.m_name from 38 student left join classinfo 39 on student.class_id=classinfo.id 40 left join major 41 on student.major_id=major.id 42 where class_id=(select class_id from student where name='張三') 43 and major_id=(select major_id from student where name='張三') 44 and student.name!='張三'; 45 -- 方法2 46 select student.name,classinfo.classname,major.m_name 47 from student 48 inner join classinfo 49 on student.class_id=classinfo.id 50 inner join major 51 on student.major_id=major.id 52 where class_id=(select class_id from student where name='張三') 53 and major_id=(select major_id from student where name='張三') 54 and student.name!='張三';