mysql中null的处理经验总结

版权声明:如有错误,请留言指正,谢谢! https://blog.csdn.net/songshuguowang/article/details/85263749

在项目中需要比对两条数据,筛选出有字段不一致的数据

select *
  from t_table t1, t_table t2
 where t1.key = t2.key
   and t1.column1 != t2.column1

其中column1是数值型且可为空,当t1中column1有值但t2中column1值为null,以上查询是不能查询出结果的

因为mysql中null不能参与任何计算与比较的,在这里可以用ifnull转换一下

select *
  from t_table t1, t_table t2
 where t1.key = t2.key
   and ifnull(t1.column1,'') != ifnull(t2.column1,'')

这样就可以了

后续待更新

猜你喜欢

转载自blog.csdn.net/songshuguowang/article/details/85263749