isnull和ifnull在mysql和sql server中的区别

    技术2022-07-10  126

    数据库中经常有字段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;

    Processed: 0.012, SQL: 9