innerjoin 内连接
select*from emp innerjoin dep on emp.dep_id=dep.id;
只连接两张表中公有的数据部分
leftjoin 左连接
select*from emp leftjoin dep on emp.dep_id=dep.id;
以左表为基准 展示左表所有的数据 如果没有对应项则用NULL填充
rightjoin 右连接
select*from emp rightjoin dep on emp.dep_id=dep.id;
以右表为基准 展示右表所有的数据 如果没有对应项则用NULL填充
union 全连接
select*from emp leftjoin dep on emp.dep_id=dep.id
unionselect*from emp rightjoin dep on emp.dep_id=dep.id;
以左右表为基准 展示所有的数据 各自没有的全部NULL填充
'''
学会了连表操作之后也就可以连接N多张表
思路:将拼接之后的表起别名当成一张表再去与其他表拼接 再起别名当一张表 再去与其他表拼接 其次往复即可
'''
方法2:子查询
将一条SQL语句用括号括起来当成另外一条SQL语句的查询条件
题目:求姓名是jason的员工部门名称
子查询类似于我们日常生活中解决问题的方式>>>:分步操作
步骤1:先根据jason获取部门编号
select dep_id from emp where name='jason';
步骤2:再根据部门编号获取部门名称
select name from dep where id=200;
总结
select name from dep where id=(select dep_id from emp where name='jason');
'''
很多时候多表查询需要结合实际情况判断用哪种 更多时候甚至是相互配合使用
'''
# 1.先确定需要用到几张表 学生表 分数表
# 2.预览表中的数据
-- select * from student;-- select * from score;
# 3.根据已知条件80分 选择切入点 分数表
# 求每个学生的平均成绩 按照student_id分组 然后avg求num即可
-- select student_id,avg(num) as avg_num from score group by student_id having avg_num>80;
# 4.确定最终的结果需要几张表 需要两张表 采用连表更加合适
-- SELECT-- student.sname,-- t1.avg_num-- FROM-- student-- INNER JOIN (-- SELECT-- student_id,-- avg(num) AS avg_num-- FROM-- score-- GROUP BY-- student_id-- HAVING-- avg_num > 80-- ) AS t1 ON student.sid = t1.student_id;
3.查询没有报李平老师课的学生姓名
# 1.先确定需要用到几张表 老师表 课程表 分数表 学生表
# 2.预览每张表的数据
# 3.确定思路 思路1:正向筛选 思路2:筛选所有报了李平老师课程的学生id 然后取反即可
# 步骤1 先获取李平老师教授的课程id
-- select tid from teacher where tname = '李平老师';-- select cid from course where teacher_id = (select tid from teacher where tname = '李平老师');
# 步骤2 根据课程id筛选出所有报了李平老师的学生id
-- select distinct student_id from score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老师'))
# 步骤3 根据学生id去学生表中取反获取学生姓名
SELECT
sname
FROM
student
WHERE sid NOTIN (
SELECTDISTINCT student_id FROM score
WHERE course_id IN (
SELECT cid FROM course
WHERE teacher_id = (
SELECT tid FROM teacher
WHERE tname ='李平老师'
)
)
)
4.查询没有同时选修物理课程和体育课程的学生姓名
# 1.先确定需要的表 学生表 分数表 课程表
# 2.预览表数据
# 3.根据给出的条件确定起手的表
# 4.根据物理和体育筛选课程id
-- select cid from course where cname in ('物理','体育');
# 5.根据课程id筛选出所有跟物理 体育相关的学生id
-- select * from score where course_id in (select cid from course where cname in ('物理','体育'))
# 6.统计每个学生报了的课程数 筛选出等于1的
-- select student_id from score where course_id in (select cid from course where cname in ('物理','体育'))-- group by student_id-- having count(course_id) = 1;
# 7.子查询获取学生姓名即可
SELECT sname FROM student
WHERE sid IN ( SELECT student_id FROM score
WHERE course_id IN (
SELECT cid FROM course WHERE cname IN ('物理', '体育')
)
GROUPBY student_id HAVINGcount(course_id) =1)
5.查询挂科超过两门(包括两门)的学生姓名和班级
# 1.先确定涉及到的表 分数表 学生表 班级表
# 2.预览表数据
-- select * from class
# 3.根据条件确定以分数表作为起手条件
# 步骤1 先筛选掉大于60的数据
-- select * from score where num < 60;
# 步骤2 统计每个学生挂科的次数
-- select student_id,count(course_id) from score where num < 60 group by student_id;
# 步骤3 筛选次数大于等于2的数据
-- select student_id from score where num < 60 group by student_id having count(course_id) >= 2;
# 步骤4 连接班级表与学生表 然后基于学生id筛选即可
SELECT
student.sname,
class.caption
FROM
student
INNERJOIN class ON student.class_id = class.cid
WHERE
student.sid IN (
SELECT
student_id
FROM
score
WHERE
num <60GROUPBY
student_id
HAVINGcount(course_id) >=2
);