《Mysql学习mysql中替代null的IFNULL()与COALESCE()函数详解》要点:
本文介绍了Mysql学习mysql中替代null的IFNULL()与COALESCE()函数详解,希望对您有用。如果有疑问,可以联系我们。
MYSQL教程在MySQL中isnull()
函数不能作为替代null值!
MYSQL教程如下:
MYSQL教程首先有个名字为business的表:
MYSQL教程
MYSQL教程
SELECT ISNULL(business_name,'no business_name') AS bus_isnull FROM business WHERE id=2
MYSQL教程直接运行就会报错:
MYSQL教程错误代码: 1582
MYSQL教程
Incorrect parameter count in the call to native function 'isnull'
MYSQL教程所以,isnull()
函数在mysql中就行不通了.可以用ifnull()
和coalesce()
代替.如下:
MYSQL教程使用ifnull()
函数:
MYSQL教程
SELECT IFNULL(business_name,'no business_name') AS bus_ifnull FROM business WHERE id=2
MYSQL教程运行结果:
MYSQL教程
MYSQL教程当查询的值不为null时:
MYSQL教程
SELECT IFNULL(business_name,'no business_name') AS bus_ifnull FROM business WHERE id=1
MYSQL教程结果如下:
MYSQL教程
MYSQL教程使用coalesce()
函数:
MYSQL教程
SELECT COALESCE(business_name,'no business_name') AS bus_coalesce FROM business WHERE id=2
MYSQL教程结果如下:
MYSQL教程
MYSQL教程当查询值不为null时:
MYSQL教程
SELECT COALESCE(business_name,'no business_name') AS bus_coalesce FROM business WHERE id=1
MYSQL教程
MYSQL教程其中:coalesce()
还可以返回第一个不为null的值.如下:
MYSQL教程
SELECT COALESCE(business_name,district_id,id) AS bus_coalesce FROM business WHERE id=2
MYSQL教程
MYSQL教程那么,isnull()
在mysql中怎么用呢?答案就是用在where后面.如下:
MYSQL教程
SELECT * FROM business WHERE ISNULL(business_name)
MYSQL教程结果如下:
MYSQL教程
MYSQL教程同样,is null
和is not null
也是用在where后面.
MYSQL教程
SELECT * FROM business WHERE business_name IS NULL
MYSQL教程结果如下:
MYSQL教程
MYSQL教程
SELECT * FROM business WHERE business_name IS NOT NULL
MYSQL教程
MYSQL教程总结
MYSQL教程以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对维易PHP的支持.
转载请注明本页网址:
http://www.vephp.com/jiaocheng/1437.html