Use of the IF condition statement in SQL

Source: Internet
Author: User

An IF expression

IF( expr1 , expr2 , expr3 )

The value of EXPR1 is TRUE, then the return value is EXPR2
The value of Expr1 is false, and the return value is EXPR3

As follows:

SELECTIF (true,1+ 1,1+2); -> 2select if (false,1 +1,1+< Span class= "Hljs-number" >2); -> 3select if (STRCMP ( "222"),  "unequal", -> not equal             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

So what's the use of this IF? As an example:
Find a book priced at 50, if it is a Java book, it should be marked as sold out
So how do you write the corresponding SQL statement?

select *,if(book_name=‘java‘,‘已卖完‘,‘有货‘) as product_status from book where price =50
    • 1
Ifnull-expression

IFNULL( expr1 , expr2 )

Returns EXPR1 if the value of EXPR1 is not true NULL , otherwise returns EXPR2, as follows:

SELECT IFNULL(NULL,"11");-> 11SELECT IFNULL("00","11");-> 00



Turn from 62467086

Use of the IF condition statement in SQL

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.