Mysql深入学习 基础篇 Ss.05多表查询语法及案例

世界总是在推着我走,我自己一个人也能站稳

                                                    —— 24.3.7  

一、多表关系

1.概述

项目开发中,在进行数据库表结构设计时,会根据业务需求及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在着各种练习,基本上分为三种:

一对多(多对一)

多对多

一对一

2.一对多(多对一)

案例:部门与员工的关系

关系:一个部门对应多个员工,一个员工对应一个部门

实现:在多的一方建立外键,指向一的一方的主键

3.多对多

案例:学生与课程的关系

关系:一个学生可以选修多门课程,一门课程也可以供多个学生选择

实现:建立第三张中间表,中间表至少包含两个外键,分别关联两方主键

第三张表除了表自己的主键还有两张‘多’表的主键当第三张表的两个外键,以此来来关联主键

4.一对一

案例:用户与用户详情的关系

关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率

实现:在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的(unique)

二、多表查询概述

1.概述:指从多张表中查询数据

2.笛卡尔积:笛卡尔乘积是指在数学中,两个集合A集合和B集合的所有组合情况(在多表查询时,需要消除无效的笛卡尔积

3.多表查询分类

连接查询

        内连接:相当于查询A、B交集部分数据

        外连接:

                左外连接:查询左表所有数据,以及两张表交集部分数据

                右外连接:查询右表所有数据,以及两张表交际部分

        自连接:当前表与自身的连接查询,自连接必须使用表别名

子查询

三、连接查询——内连接

内连接查询语法:

①隐式内连接

select 字段列表 from 表1,表2 where 条件…;

②显示内连接

select 字段列表 from 表1 [inner] join 表2 on 连接条件…;

内连接查询的是两张表交集的部分

四、外连接

外连接查询语法:

①左外连接(包含左表所有数据)

select 字段列表 from 表1 left [outer] join 表2 on 条件…;

相当于查询表1(左表)的所有数据包含表1和表2交集部分的数据

②右外连接(包含右表所有数据)

select 字段列表 from 表1 right [outer] join 表2 on 条件…;

相当于查询表2(右表)的所有数据包含表1和表2交集部分的数据

五、自连接

自连接查询语法:

select 字段列表 from 表A 别名A join 表A 别名B on 条件…;

案例1:

案例2:

自连接查询,可以是内连接查询,也可以是外连接查询

六、联合查询-union,union all

对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集

select 字段列表 from 表A…
union [all]
select 字段列表 from 表B…;

案例:

1.将薪资低于5000的员工,和年龄大于50岁的员工全部查询出来

select * from emp where salary < 5000
union all
select * from emp where age > 50;

对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致

union all会将全部的数据直接合并在一起,union会对合并之后的数据去重

七、子查询

1.概念:SQL语句中嵌套select语句,称为嵌套查询,又称子查询

select * from t1 where column1 = (select column1 from t2);

子查询外部的语句可以是insert/update/delete/select的任何一个

2.根据子查询结果不同,分为:

标量子查询(子查询结果为单个值)

列子查询(子查询结果为一列)

行子查询(子查询结果为一行)

表子查询(子查询结果为多行多列)

3.根据子查询位置,分为:where之后,from之后,select之后

4.标量子查询

子查询返回的结果单个值(数字、字符串、日期等),最简单的形式,这种子查询称为标量子查询

常用的操作符:= <> > >= < <=

案例

1>

2>

5.列子查询

子查询返回的结果是一列(可以是多行),这种子查询称为列子查询

常用的操作符:in、not in、any、some、all

                操作符                                描述

                  in                                在指定的集合范围之内,多选一

               not in                             不在指定的集合范围之内

                any                               子查询返回列表中,有任意一个满足即可

                some                            与any相同,使用some的地方可以使用any

                 all                                子查询返回列表的所有值都必须满足        

案例:1.查询“销售部”和“市场部”所有的员工信息

select * from emp where dept_id in (select id from dept where name = '销售部' or name = '市场部');

 2.查询比财务部所有人工资都高的员工信息

select * from emp where salary > all (select salary from emp where dept_id = (select id from dept where name = '财务部'));

 3.查询比研发部其中任意一人工资高的员工信息

select * from emp where salary > any (select salary from emp where dept_id = (select id from dept where name = '研发部'));

any可换为some

6.行子查询

子查询返回的结果是一行(可以是多列),这种子查询称为行子查询

常用的操作符:=、<>、in、not in

案例:

1.查询与“张无忌”的薪资及直属领导相同的员工信息

select * from emp where (salary,managerid) = (select salary,managerid from emp where name = '张无忌');

7.表子查询

子查询返回的结果是多行多列的数据,这种子查询称为表子查询

常用的操作符:in

案例:

1.查询与“鹿杖客”,“宋远桥”的职位与薪资相同的员工信息

select * from emp where (job,salary) in (select job,salary from emp where name = "宋远桥" or name = "鹿杖客");

2.查询入职信息是“2006-01-01”之后的员工信息,及其部门信息

select * from (select * from emp where entrydate > '2006-01-01') as e left join dept as d on e.dept_id = d.id;

八、多表查询案例

1.查询员工的姓名、年龄、职位、部门信息。

-- 表:emp,dept

-- 连接条件:emp.dept_id = dept.id

select e.name,e.age,e.job from emp e,dept d where e.dept_id = d.id;


2.查询年龄小于30岁的员工姓名、年龄、职位、部门信息。

-- 表:emp,dept

-- 连接条件:emp.dept_id = dept.id

select e.name,e.age,e.job,d.name from emp e inner join dept d on e.dept_id = d.id where e.age < 30;


3.查询拥有员工的部门ID、部门名称。

-- 表:emp,dept

-- 连接条件:emp.dept_id = dept.id

select distinct d.id, d.name from emp e,dept d where e.dept_id = d.id;
#distinct 去除重复查询记录


4.查询所有年龄大于40岁的员工,及其归属的部门名称;如果员工没有分配部门,也需要展示出来。

-- 表:emp,dept

-- 连接条件:emp.dept_id = dept.id

-- 外连接

select e.*,d.name from emp e left joinn dept d on e.dept_id = d.id where e.age > 48;


5.查询所有员工的工资等级。

-- 表:emp,salgrade

-- 连接条件:emp.salary >= salgrade.losal and emp.salary <= salgrade.hisol

select e.*,s.grade from emp e,salgrade s where e.salary >= s.losal and e.salary <= s.hisal;

select e.*,s.grade from emp e,salgrade s where e.salary between s.losal and s.hisal;


6.查询"研发部"所有员工的信息及工资等级。

-- 表:emp,dept,salgrade

-- 连接条件:emp.salary between salgrade.level and salgrade.hisal,emp.dept_id = dept.id

-- 查询条件:dept.name='研发部'

select e.*,s.grade 
from emp e,dept d,salgrade s 
where e.dept_id = d.id and 
(e.salary >= s.losal and e.salary <= s.hisal) 
and d.name = '研发部';


7.查询"研发部"员工的平均工资。

-- 表:emp,dept

-- 连接条件:emp.dept_id = dept.id

select avg(e.salary) from emp e,dept d where e.dept_id = d.id and d.name = '研发部';


8.查询工资比"灭绝"高的员工信息。

select * from emp where salary > (select salary from emp where name = '灭绝');


9.查询比平均薪资高的员工信息。

select * from emp where salary > (select avg(salary) from emp);


10.查询低于本部门平均工资的员工信息。

select * from emp e2 where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id);


11.查询所有的部门信息,并统计部门的员工人数。

select d.id,d.name,(select count(*) from emp e where e.dept_id = id) as '人数' from dept d;


12.查询所有学生的选课情况,展示出学生名称,学号,课程名称

-- 表:student,course,student_course

-- 连接条件:student.id = student_course.studentid,course.id = student_course.courseid

select s.name,s.no,c.name from student as s,student_course as sc,course as c where s.id = sc.studentid and sc.courseid = c.id;

九、总结

1.多表关系

一对多:在多的一方设置外键,关联另一方的主键

多对多:建立中间表,中间表包含两个外键来关联另外两张表的主键

一对一:用于表结构拆分,在其中任何一方设置外键(unique),关联另一方的主键

2.多表查询

内连接

        隐式内连接:select…from 表A ,表B where 条件…

        显示内连接:select…from 表A inner join 表B on 条件…

外连接:

        左外连接:select …from 表A left join 表B on 条件…

        右外连接:select …from 表B right join 表B on 条件…

自连接:select … from 表A 别名1,表A 别名2 where 条件…

子查询:标量子查询,列子查询,行子查询,表子查询

  • 25
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值