jkun–结合官方手册,解释关键字like在MySQL字段null值上的坑
author:陈镇坤27
阅读时间:3分钟
摘要:结合官方手册、解释在“expr like pat”的语句下,可能产生的坑,并进行猜想
关键字:Null值、like、not like
——————————————————————————————
问题语句:

因为short_name为null值,sql匹配条件要求查询short_name not like ‘%emdtest%’,所以查询不出结果了。
解决思路:or short _name is null
MySQL5.7手册说明:

解释:short_name not like ‘%emdtest%’。本质是获取short_name 列的值,再与表达式中的值做匹配。关键词not like一直发挥作用。
当short_name的值为null时,则意味null not like ‘%emdtest%’。
则意味着 null
而MySQL对null的定义如下

所以,语句也就变成了 where a = 10 and b = 12 and null
则等价于 where null
则等价于 where 没有数据
所以返回空。
where null
则等价于 where 没有数据
所以返回空。