Blogger Information
Blog 56
fans 7
comment 11
visits 223139
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysql 判断null和空字符串
樂成的开发笔记
Original
6431 people have browsed it

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

2.对null 的判断:

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

  select * from user;

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

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

 1092396-20180701211108185-952991217.png

  

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

 过滤到null的sql 语句 还用可以用  select * from user where name is not null;

1092396-20180701212352409-85807253.png

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

1092396-20180701212445236-543734983.png

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

select * from user where ISNULL(name)=0 and LENGTH(trim(name))>0;
1092396-20180701212719052-1368208492.png

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

实例

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

运行实例 »

点击 "运行实例" 按钮查看在线实例

1092396-20180701213605962-236975422.png    



参考资料

 


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post