on and 和 on where 的 区别

在使用 left join 时, on and 和 on where 会有区别;
1. on的条件是在连接生成临时表时使用的条件,以左表为基准 ,不管on中的条件真否,都会返回左表中的记录
  on 后面 and 都是对右表进行筛选

2.where是全部连接完后,对临时表进行筛选,筛选对左表和右表都有效

在使用inner join时,on   and与on  where条件的区别:不管是对左表还是右表进行筛选,on  and和on  where都会对生成的临时表进行过滤。

1. 新建表 t1

SQL  on and 和 on where 的区别

2. 新建表 t2

SQL  on and 和 on where 的区别

3. 关联查询:

select * from t1 left join t2 on t1.user_id = t2.user_id

SQL  on and 和 on where 的区别

on and 

a.) 对左表加and 条件

select * from t1 left join t2 on t1.user_id = t2.user_id and t1.deleted = 0

SQL  on and 和 on where 的区别

 

b.) 对右表加and 条件

select * from t1 left join t2 on t1.user_id = t2.user_id and t2.deleted = 0

SQL  on and 和 on where 的区别

 

 on where

select * from t1 left join t2 on t1.user_id = t2.user_id where t1.deleted = 0

SQL  on and 和 on where 的区别

select * from t1 left join t2 on t1.user_id = t2.user_id where t2.deleted = 0

SQL  on and 和 on where 的区别

 

相关文章:

  • 2021-10-06
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-19
  • 2021-10-30
  • 2021-11-12
相关资源
相似解决方案