今天工作的时候碰见了一个非常神奇的问题,简单来说我写的SQL是这样的
复制代码
1
2
3
4
5
6
7
8
9
10
11
12select f.id, p.id from table1 as f full join table2 as p on f.id = p.id where f.date >= '{}' and p.date >= '{}' group by 1, 2
其中我用到了full join
就是想要看下两张表中有多少没有join上的。
但是得到的结果中发现只有两个表完全join上的结果,没有join结果为null的结果。
于是我就写了下面这个SQL:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27select f.id, p.id from ( select id from table1 where date >= '{}' group by 1 ) as f full join ( select id from table2 where date >= '{}' group by 1 ) as p on f.id = p.id
这样的话就没有问题了
问了组里的另一个dev,才意识到上面那个join的问题所在。
因为上面的写法中,是先join在做where比较的,所以当时full join完应该是没有问题的,但是等到后面where的时候,因为join出来为null的结果,其where过滤条件肯定为false的,所以最后经过where过滤之后不存在为null的结果了
最后
以上就是坦率毛衣最近收集整理的关于SQL中join加where时的一个小坑的全部内容,更多相关SQL中join加where时内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复