mysql 判断字段是否为null

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(age,0))  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;

猜你喜欢

转载自blog.51cto.com/791165566/2421060