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
2. 新建表 t2
3. 关联查询:
select * from t1 left join t2 on t1.user_id = t2.user_id
on and
a.) 对左表加and 条件
select * from t1 left join t2 on t1.user_id = t2.user_id and t1.deleted = 0
b.) 对右表加and 条件
select * from t1 left join t2 on t1.user_id = t2.user_id and t2.deleted = 0
on where
select * from t1 left join t2 on t1.user_id = t2.user_id where t1.deleted = 0
select * from t1 left join t2 on t1.user_id = t2.user_id where t2.deleted = 0