饿虎岗资源网 Design By www.oxmxm.com

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

如下:

首先有个名字为business的表:

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

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

运行结果:

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

当查询的值不为null时:

SELECT IFNULL(business_name,'no business_name') AS bus_ifnull FROM business WHERE id=1

结果如下:

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

使用coalesce()函数:

SELECT COALESCE(business_name,'no business_name') AS bus_coalesce FROM business WHERE id=2

结果如下:

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

当查询值不为null时:

SELECT COALESCE(business_name,'no business_name') AS bus_coalesce FROM business WHERE id=1

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

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

SELECT COALESCE(business_name,district_id,id) AS bus_coalesce FROM business WHERE id=2

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

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

SELECT * FROM business WHERE ISNULL(business_name)

结果如下:

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

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

SELECT * FROM business WHERE business_name IS NULL

结果如下:

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

SELECT * FROM business WHERE business_name IS NOT NULL

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

总结

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

饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com