欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL的IFNULL()、ISNULL()和NULLIF()函数实例讲解

程序员文章站 2022-03-09 22:10:20
在使用left join等联表查询时,常遇到某些字段为null,一般都在后台语言使用if (a==null) 判断做处理,其实MySQL本身也有一个IFNULL函数可以处理。同时我...

在使用left join等联表查询时,常遇到某些字段为null,一般都在后台语言使用if (a==null) 判断做处理,其实MySQL本身也有一个IFNULL函数可以处理。同时我们也来说说ISNULL()和NULLIF()函数。

分享

一、IFNULL(expr1,expr2)用法

假如expr1不为NULL,则 IFNULL() 的返回值为expr1;否则其返回值为 expr2。IFNULL()的返回值是数字或是字符串,具体情况取决于其所使用的语境。

      mysql>   SELECT   IFNULL(1,0);   
                            ->   1   
      mysql>   SELECT   IFNULL(NULL,10);   
                            ->   10   
      mysql>   SELECT   IFNULL(1/0,10);   
                            ->   10   
      mysql>   SELECT   IFNULL(1/0,'yes');   
                            ->   'yes'  

IFNULL(expr1,expr2)的默认结果值为两个表达式中更加“通用”的一个,顺序为STRING、REAL或 INTEGER。

项目中具体代码

  public static List<ta_activity_info> ChooseByActivityID(string ActivityID)  
      {  
          string strSql = @"SELECT DISTINCT  
    @ActivityID as ActivityID,  
   IFNULL(ta_choujiang.CreateTime,(SELECT ta_activity_info.CreateTime from ta_activity_info where ta_activity_info.ActivityID=@ActivityID))as CreateTime,  
    (  
        SELECT  
            count(*)  
        FROM  
            ta_choujiang  
        WHERE  
            ta_choujiang.ActivityID = @ActivityID  
    ) AS PeopleSum,  
    (  
        SELECT  
            count(*)  
        FROM  
            ta_wininfo  
        WHERE  
            ta_wininfo.ActivityID = @ActivityID  
    ) AS zhongjiangPeople,  
    (  
        SELECT  
            (  
                (  
                    SELECT  
                        count(*)  
                    FROM  
                        ta_choujiang  
                    WHERE  
                        ta_choujiang.ActivityID = @ActivityID  
                ) - (  
                    SELECT  
                        count(*)  
                    FROM  
                        ta_wininfo  
                    WHERE  
                        ta_wininfo.ActivityID =@ActivityID  
                )  
            )  
    ) AS weizhongjiangPeople  
FROM  
    ta_activity_info  
LEFT JOIN ta_choujiang ON ta_activity_info.ActivityID = ta_activity_info.ActivityID  
and ta_choujiang.ActivityID =@ActivityID";  
  
          //参数赋值  
          MySqlParameter[] param =  
                       {  
                          new MySqlParameter("@ActivityID", MySqlDbType.VarChar)  
                       };  
          param[0].Value = ActivityID.ToString();  
          //return GetList2<ta_rewardinfo>(strSql, param);  
          List<ta_activity_info> List = GetListV3<ta_activity_info>(strSql, param);  
          return List;  
      }  
  public static List<ta_activity_info> ChooseByActivityID(string ActivityID)  
      {  
          string strSql = @"SELECT DISTINCT  
    @ActivityID as ActivityID,  
   IFNULL(ta_choujiang.CreateTime,(SELECT ta_activity_info.CreateTime from ta_activity_info where ta_activity_info.ActivityID=@ActivityID))as CreateTime,  
    (  
        SELECT  
            count(*)  
        FROM  
            ta_choujiang  
        WHERE  
            ta_choujiang.ActivityID = @ActivityID  
    ) AS PeopleSum,  
    (  
        SELECT  
            count(*)  
        FROM  
            ta_wininfo  
        WHERE  
            ta_wininfo.ActivityID = @ActivityID  
    ) AS zhongjiangPeople,  
    (  
        SELECT  
            (  
                (  
                    SELECT  
                        count(*)  
                    FROM  
                        ta_choujiang  
                    WHERE  
                        ta_choujiang.ActivityID = @ActivityID  
                ) - (  
                    SELECT  
                        count(*)  
                    FROM  
                        ta_wininfo  
                    WHERE  
                        ta_wininfo.ActivityID =@ActivityID  
                )  
            )  
    ) AS weizhongjiangPeople  
FROM  
    ta_activity_info  
LEFT JOIN ta_choujiang ON ta_activity_info.ActivityID = ta_activity_info.ActivityID  
and ta_choujiang.ActivityID =@ActivityID";  
  
          //参数赋值  
          MySqlParameter[] param =  
                       {  
                          new MySqlParameter("@ActivityID", MySqlDbType.VarChar)  
                       };  
          param[0].Value = ActivityID.ToString();  
          //return GetList2<ta_rewardinfo>(strSql, param);  
          List<ta_activity_info> List = GetListV3<ta_activity_info>(strSql, param);  
          return List;  
      }  

二、ISNULL(expr) 的用法

如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0。

    mysql> select isnull(1+1);
                       -> 0
    mysql> select isnull(1/0);
                       -> 1

使用= 的null 值对比通常是错误的。

三、NULLIF(expr1,expr2)用法

如果expr1= expr2 成立,那么返回值为NULL,否则返回值为expr1。这和CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END相同。

    mysql>   SELECT NULLIF(1,1);   
                     ->   NULL   
mysql>   SELECT   NULLIF(1,2);   
                     ->   1  

如果参数不相等,则 MySQL 两次求得的值为 expr1。