The if () function in MySQL uses

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.