阅读原文:https://www.cnblogs.com/blogxiao/p/9251460.html

1. 在mysql中 null 不能使用任何运算符与其他字段或者变量(函数、存储过程)进行运算。若使用运算数据就可能会有问题。

2. 对null 的判断:

  创建一个user表:id 主健 name 可以为空

  select * from user;

  insert into user values('33',null);  ##创建一条name为空的数据

  insert into user values('222','');  ##创建一条为空字符的数据

  

用 isnull 判断是否为空:只有 name 为 null 的时候 ISNULL(exp) 函数的返回值为1,空串和有数据都为0;

过滤掉 null 的sql 语句,还可以这样写:select * from user where name is not null;

或者  select * from user where ISNULL(name)=0;

3. 同时剔除 null 和 空字符串 

select * from user where ISNULL(name)=0 and LENGTH(trim(name))>0;

4. 在函数或者存储过程中判断是否为 null 或者 空字符串

1
2
3
4
5
6
SELECT id,name,
    CASE
        WHEN (ISNULL(NAME)=1) || (LENGTH(trim(NAME))=0) THEN  'aaa'
    END
FROM
    USER

 

 

posted on 2021-04-14 18:46  51core  阅读(796)  评论(0)    收藏  举报