mysql 判断null 和 空字符串

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 或者 空字符串

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

 

猜你喜欢

转载自www.cnblogs.com/blogxiao/p/9251460.html
今日推荐