sql 中的null 值是不等于null的,当你在使用sql左连接查询的时候(类似连接),即使途中这两条记录的name,year,month 是一致的(包含null)但在sql 筛选出来的结果是认为不一致的
tips: sql 中:
- null !=null --> true
- ‘222’ != null --> true
因此在做逻辑运算的时候,如果有可能出现空值,需要将空值替换成默认值,否则会出现一些意想不到的结果
select * from (SELECT * from b where d='2019-07-02') c
left join (select * from b where d='2019-07-01') d
on (c.name =d.name and c.year=d.year and c.month= d.month)
where d.name is null
执行结果:
解决办法:
select * from (SELECT * from b where d='2019-07-02') c
left join (select * from b where d='2019-07-01') d
on (c.name =d.name and ISNULL(c.year)=ISNULL(d.year) and c.month= d.month)
where d.name is null
执行结果: