• 技术文章 >数据库 >MySQL

    mysql中的if函数怎么用

    yangyang2020-05-08 16:04:30原创1851

    在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 | 女  |
    +-------+-----+
    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教程

    专题推荐:mysql
    上一篇:mysql在cmd命令行出现乱码如何解决 下一篇:mysql的ini文件在哪?

    相关文章推荐

    • mysql与mssql的区别是什么• mysql内存不足怎么解决

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网