当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql中替代null的IFNULL()与COALESCE()函数详解

mysql中替代null的IFNULL()与COALESCE()函数详解

2017年12月07日  | 移动技术网IT编程  | 我要评论

在mysql中isnull()函数不能作为替代null值!

如下:

首先有个名字为business的表:

select isnull(business_name,'no business_name') as bus_isnull from business where id=2

直接运行就会报错:

错误代码: 1582

incorrect parameter count in the call to native function 'isnull'

所以,isnull()函数在mysql中就行不通了。可以用ifnull()coalesce()代替。如下:

使用ifnull()函数:

select ifnull(business_name,'no business_name') as bus_ifnull from business where id=2

运行结果:


当查询的值不为null时:

select ifnull(business_name,'no business_name') as bus_ifnull from business where id=1

结果如下:


使用coalesce()函数:

select coalesce(business_name,'no business_name') as bus_coalesce from business where id=2

结果如下:

 

当查询值不为null时:

select coalesce(business_name,'no business_name') as bus_coalesce from business where id=1

其中:coalesce()还可以返回第一个不为null的值。如下:

select coalesce(business_name,district_id,id) as bus_coalesce from business where id=2

那么,isnull()在mysql中怎么用呢?答案就是用在where后面。如下:

select * from business where isnull(business_name)

结果如下:


同样,is null is not null 也是用在where后面。

select * from business where business_name is null

结果如下:

select * from business where business_name is not null

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网