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`))
来源:PY学习网:原文地址:https://www.py.cn/article.html