mysql查询字段为null、不为null的数据
对于null字段的操作,有几点注意的地方,统一收集记录在此文章,以便后面的查找使用。
1.查询表中某一字段为null的结果
select * from table1 where column1=null; #错误使用方法:查询结果为空 select * from table1 where isnull(column1); #正确使用方法,注意isnull的大小写。isnull就会报语法错误。
2.查询表中某一字段不为null的结果
select * from table1 where column1 is not null;
3.null与其他字符的拼接、运算、比较操作
select null+"aa"; #结果为null select concat(null,"a"); #null与其他字符做拼接,结果为null select null+1; #null与其他字符做运算,结果为null select null<>0; #null与其他字符做比较,结果为null
在查询数据时,条件是某个字段不为空。
在这里用密码不为空来举例:
select * from user where password != null
可以看到在我的数据库里面,user表是有数据的
但是点击运行后,却没有任何结果。
接下来把查询语句里面的 != 换成 is not
select * from user where password is not null
会发现查询到了数据,用 != 查不到数据,用 is not null 才可以
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论