type
Post
status
Published
date
Jul 31, 2024
slug
summary
tags
category
icon
password
😀
今天写了2个表之间的left join在on条件加了两个条件其中一个为指定值,结果发现结果不是所预期的 例子: select t1.name as name1, t2.name as name2 from test_t1 t1 left join test_t2 t2 on t1.num= t2.num and t1.year = 2024 结果下面是不一样的 select t1.name as name1, t2.name as name2 from test_t1 t1 left join test_t2 t2 on t1.num = t2.num where t1.year = 2024 结果1:
name1
name2
a
a
b
b
c
c
a
null
b
null
c
null
结果2:
name1
name2
a
a
b
b
c
c

观点

两个表left join 只是将数据联合,不符合条件的只是联合不到数据,并不会将数据过滤,而where会通过条件过滤得出结果 如例子中 结果1:是左联合查询则整体数据是以左为主体与右得出笛卡尔积,而条件1,两者num相等数据合在一起,条件2,t1.year=2024,其中t1.year=2023不符合就不会联合t2表所以name2是null 结果2:是联合后对数据进行过滤,(select t1.name as name1, t2.name as name2 from test_t1 t1 left join test_t2 t2 on t1.num = t2.num)联合后的数据是
name1
nam2
a
a
b
b
c
c
a
a
b
b
c
c
但其中有三行year=2023,所以不满足的数据会被过滤得到结果2

🤗 总结归纳

联合查询的条件是联合数据的条件,不会过滤数据,可以通过where条件对结果集进行过滤
 
 
💡
有关使用上的问题,欢迎您在底部评论区留言,一起交流~
Spring之三级缓存MySQL视图
Loading...