1. select xxx
  2. from tableA, tableB
  3. where tableA.name = "a"
  4. and tableA.id = tableB.id
  5. and tableB.name="b"
  • tableA 是驱动表,用于数据提供
  • tableB 是被驱动表,用于数据关联
  • 操作
    • tableA.name 从 tableA 中索引过滤出一批数据
    • tableB.name 从 tableB 中索引过滤出一批数据
    • tableA.id = tableB.id 将上面两组数据进行数据关联

驱动表

  1. left join 左表是驱动表
  2. right join 右表是驱动表
  3. join mysql 优化器会选择小表为驱动表

inner join, outer join

  1. inner join 关联条件是 on or where
    1. where 和 on 是等价的,即 mysql 优化器会将 on 优化为 where
  2. left join 和 right join 关联条件就是 on
    1. 至于 where 和 on 的选择,需要根据实际业务
    2. 比如 left join 下,on 操作不管条件是否满足,都会返回左边数据,然后根据 where 进行查询