mysql 中 isnull 和 ifnull 判断字段是否为null

数据库中经常有字段type为null ,对于统计count(type)和avg(type) 都不起作用


SQL中有ISNULL方法,介绍如下:


ISNULL
使用指定的替换值替换 NULL。


语法
ISNULL ( check_expression , replacement_value )


参数
check_expression
将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。
replacement_value
在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。


例如:


SELECT count(ISNULL(Weight, 50))  FROM  Product;


但是在mysql中,isnull只是用来判断是否为空,不能实现替换功能,照上面写的话,会直接报错(Incorrect parameter count in the call to native function 'isnull' Errornumber:1582 )。


那么Mysql中如何实现SQL中的ISNULL方法呢?IFNULL( check_expression , replacement_value ),实现了SQL中的ISNULL方法。


 还是上面的例子:


SELECT count(IFNULL(Weight, 50))  FROM  Product;


转自:http://blog.csdn.net/cxqiang2013/article/details/35587295

猜你喜欢

转载自blog.csdn.net/qq_35448976/article/details/79497123