表连接学习笔记

类型

  • 内连接
  • 外连接
    • 左外连接
    • 右外连接
左外连接语法

select * from t1 left [outer] join t2 on 连接条件 [where 普通过滤条件]

右外连接语法

select * from t1 right [outer] join t2 on 连接条件 [where 普通过滤条件]

内连接语法

select * from t1 [inner | cross] join t2 [on 连接条件] [where 普通过滤条件]

也就是说在mysql中,下面这几种内连接的写法都是等价的:

select * from t1 join t2;
select * from t1 inner join t2;
select * from t1 cross join t2;
select * from t1, t2

where 子句中的过滤条件

  • where 子句中的过滤条件,不论是内连接还是外连接,凡是不符合条件的记录都不会被加入到最后的结果集

on 子句中的过滤条件

  • 对于外连接的驱动表中的记录来说,如果无法在被驱动表中找到匹配on子句中过滤条件的记录,那么该驱动表记录仍然会被加入到结果集中,对应的被驱动表记录的各个字段使用NULL值填充
  • 内连接中的on子句和where子句的执行结果是等价的

posted on 2022-11-17 11:01  每日问答  阅读(25)  评论(0)    收藏  举报

导航