MySQL多表查询与事务
图
MySQL多表查询是指在一个SQL语句中涉及到多个表,可以通过JOIN操作将这些表连接起来,以实现更复杂的数据查询。MySQL支持多种JOIN操作,包括INNER JOIN、LEFT JOIN、RIGHT JOIN等

表联系

  • 一对一
  • 一对多(多对一)
  • 多对多

通常多对多的关系会有一张单独的关系表

连接查询

  • 内连接:查询A、B交集数据
  • 外连接
    • 左外连接:查询左表所有数据,以及两张表的交集数据
    • 右外连接:查询右表所有数据,以及两张表的交集数据
  • 自连接:与自身表连接(必须使用表别名,否则无法区分表)
-- 内连接
select * from a,b where a.id=b.aid

-- 左外连接
select a.*,b.name from a left join b on a.id = b.aid

-- 右外连接
select a.*,b.name from a right join b on a.id = b.aid

联合查询

-- union all
select * from a where xxx
union all
select * from a where xxx


-- union
select * from a where xxx
union
select * from a where xxx

union all将上下两个数据直接拼接,union会去重,注意上下查询字段需要一致才能合并

子查询

标量子查询:子查询的结果是个单值,结果可直接用于其他查询,支持的操作符:= > < >= <= <><>是不等于

-- 查询技术部的员工
select * from emp where dept_id = (select id from dept where name = '技术部')
-- 查询某某之后入职的员工
select * from emp where entrydate > (select entrydate from emp where name = '某某')

列子查询:子查询的结果为多个数据(一列),支持的操作符:in not in any some all

操作符 说明
in 在集合范围内
not in 不在集合范围内
any 子查询返回列表中,有任意一个满足即可
some 与any相同
all 子查询返回列表的所有值都要满足
-- 查询“技术部”和“财务部”的所有员工
select * from emp where dept_id in (select id from dept where name = '技术部' or name = '财务部')
-- 查询比“财务部”所有人工资都高的员工信息
select * from emp where salary > all (select salary from emp where dept_id = (select id from dept where name = '财务部'))

行子查询:子查询返回结果为多列一行(多个字段),支持操作符= <> in not in

-- 查询与某某的薪资及直属领导相同的员工信息
select * from emp where (salary,manager_id) = (select salary, manager_id from emp where name = '某某')

表子查询:子查询返回的结果为多列多行,支持操作符in

--查询与“某某一”、“某某二”的职位和薪资相同的员工信息
select * from emp where (job,salary) in (select job,salary from emp where name = '某某一' or name = '某某二')

事务

事务的四大特性:

  • 原子性:事务是不可分割的最小操作单元,要么都成功,要么都失败
  • 一致性:事务完成时,必须使所有的数据都保持一致的状态
  • 隔离性:数据库系统提供的隔离机制,保证事务在不受外部并发操作影响的独立环境下运行
  • 持久性:事务一旦提交或回滚,对数据库中数据的改变时永久性的

并发事务问题:

  • 脏读:一个事务读到另一个事务还没有提交的数据
  • 不可重复读:一个事务先后读取同一条数据,但两次读取的数据不同
  • 幻读:一个事务按照条件查询数据时没有对应的数据行,但是插入数据时又发现该行数据已存在

事务隔离级别:

隔离级别 脏读 不可重复读 幻读
Read uncommitted
Read committed x
Repeatable Read(默认) x x
Serializable x x x

Serializable隔离级别能解决所有事务问题,但是性能不高。即隔离级别越高数据越安全,但是性能越低

-- 查看事务隔离级别
select @@transaction_isolation
-- 设置事务隔离级别
set [session|global] transaction isolation level {级别}