mysql中if()函数使用

博主原创,转载请注明出处:

      在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下:

IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,

则返回expr3的值。

       其经常判断查询出来的值,示例;

mysql> select name,if(sex=0,'','') as sex from student;
+-------+-----+
| name  | sex |
+-------+-----+
| name1 | 女  |
| name2 | 女  |
| name3 | 男  |
| name4 | 女  |
+-------+-----+
4 rows in set (0.00 sec)

       其也经常用到判断的关联条件中,其示例如下:

SELECT  s.SCHOOL_CITY as schoolCity,
        s.SCHOOL_COUNTY as schoolCounty,
        count(DISTINCT `s`.`SCHOOL_ID`) as schoolNum,
        sum(m.duration) as sumDuration,
        sum(`m`.`VIEWERCOUNT`) as viewLiveSum,
        sum(m.replayViewerCount)as reViewSum,
        sum(m.praisecount) as sumpraise,
        sum(`m`.`VIEWERCOUNT`+m.replayViewerCount) as viewSum
         from ((tbl_hbb_mobile_live_statistics m join tbl_hbb_resource_visit_map v) join tbl_school_info s)
    where`m`.`RESOURCEID`= `v`.`RESOURCEID` 
        and if((`v`.`rangeTYPE`= '2'),(`v`.`rangeID`= `s`.`SCHOOL_ID`),(`v`.`parentId`= `s`.`SCHOOL_ID`))

 

    原文作者:mysql
    原文地址: https://www.cnblogs.com/zjdxr-up/p/8383609.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞