MySQL SQL语句-is、is not判断NULL 作者:马育民 • 2021-02-05 21:56 • 阅读:10137 需要掌握:[MySQL NULL值](https://www.malaoshi.top/show_1GW2ICBt2HJt.html "MySQL NULL值") # 提出问题 emp表中很多员工的comm字段值为空,其实是 **NULL**,而 **不是空字符串** 要查询 `comm` **为空** 的记录,**不能使用** `comm = ''`,如下: ``` select * from emp where comm = '' ``` 查询的结果,是 `空字符串`,而不是 `null` 值 ### 分析 由于 `NULL` 的特殊性,普通比较运算符(`=`、`>`、`<`、`LIKE` 等)无法正确处理 `NULL` ,**不会返回任何记录**,**且不会报错** ### 解决 使用专用语法 `is`、`is not` # NULL 的判断 | 操作 | 正确写法 | 错误写法 | |-----------------------|---------------------------|---------------------------| | 判断字段为 NULL | `column IS NULL` | `column = NULL` | | 判断字段不为 NULL | `column IS NOT NULL` | `column != NULL` | | NULL 值的安全比较 | `column <=> NULL` | `column = NULL` | ### 解决 应该用 `is null`: ``` select * from emp where comm is null ``` 查询 comm **不为空** 的信息,如下: ``` select * from emp where comm is not null ``` 原文出处:http://malaoshi.top/show_1IXXR9qMp4E.html