Bo Master Original, reproduced please indicate the source:
The use of the IF () function in MySQL is similar to the three-mesh expression in Java, and is more useful, with the following syntax:
if (EXPR1,EXPR2,EXPR3), if the value of EXPR1 is true, the value of EXPR2 is returned, if the value of Expr1 is false,
The value of EXPR3 is returned.
It often judges the value of the query, example;
Select name,if(sex=0,' female ',' male ' as from student; +-------+-----+| Name | sex |+-------+-----+| name1 | female | | name2 | female | | name3 | male | | name4 | female |+-------+-----+
4
in
set (
0.00 sec)
It is also often used to determine the associated conditions, which are examples of the following:
SELECT s.school_city asschoolcity, S.school_county asSchoolcounty, Count (DISTINCT ' s '. ' school_id ') asschoolnum, sum (m.duration) assumduration, sum (' m '. ' Viewercount ') asviewlivesum, sum (m.replayviewercount) asreviewsum, sum (m.praisecount) assumpraise, sum (' m '. ' Viewercount ')+m.replayviewercount) asViewsum from((tbl_hbb_mobile_live_statistics m join Tbl_hbb_resource_visit_map v) join Tbl_school_info s)where' m '. ' RESOURCEID ' =' V '. ' RESOURCEID ' andif(' V '. ' RangeType ' ='2'), (' V '. ' Rangeid ' = ' s '. ' school_id '), (' V '. ' ParentID ' = ' s '. ' school_id '))
The if () function in MySQL uses