SQL参数放在where前后的区别

时间:2023-03-09 09:00:06
SQL参数放在where前后的区别

本博客记录一个细节,在使用sql left join时候,参数放在left join后面当条件,还是放在where后的区别

给出两条SQL:

tt.book_type = 'TIPS_TYPE',放在left join后面当条件

select tc.seq,
tc.tips_flag,
tc.is_valid,
tc.create_time,
tt.book_name tipsType
from t_tips_config tc
left join t_book tt
on tt.book_code = tc.tips_flag
and tt.book_type = 'TIPS_TYPE'

tt.book_type = 'TIPS_TYPE',放在where后面当条件

select tc.seq,
tc.tips_flag,
tc.is_valid,
tc.create_time,
tt.book_name tipsType
from t_tips_config tc
left join t_book tt
on tt.book_code = tc.tips_flag
where tt.book_type = 'TIPS_TYPE'

这两种情况意义完全不一样的,前者如果t_book没有book_type = 'TIPS_TYPE'的数据,整条SQL还是可以查到数据的,只是t_book的参数没查到而已,后者,一旦出现book_type = 'TIPS_TYPE'没有数据,那就整条SQL都查不到数据,这样是不合理的,因为业务需要查出t_tips_config的表,不然就不会用左连接了

这是一个小细节,记录一下