MySQL's if,case use notes

Source: Internet
Author: User

In an SQL expression:

An If expression:

if (EXPR1,EXPR2,EXPR3)

  If Expr1 is true (expr1 <> 0 and Expr1 <> NULL), then the return value of if () is expr2; Otherwise the return value is EXPR3. The return value of IF () is a numeric value or a string value, depending on the context in which it is located.

Select *,if (sva=1, "male", "female") as Ssva from Taname where SVA! = ""

   When SVA returns to the male at 1 o'clock, the woman is returned.

Ifnull expression:

Ifnull (EXPR1,EXPR2)

 When Expr1<>null, return expr1, otherwise return EXPR2, can be nested:

Ifnull (Ifnull (Expr1,ifnull (...)), Ifnull (...)

  Case-When expression:

Two types of formats:

The simple case function compares an expression to a set of simple expressions to determine the result.

The case search function calculates a set of Boolean expressions to determine the result.

The optional ELSE parameter is supported in both formats.

A complete simple case

Case input_expression while    when_expression then result_expression        [... n]    [         ELSE Else_result_ Expression    END

  

When input_expression = when_expression, returns result_expression

Such as

Select (Case Aix if '     s400 ' then ' hp ' when    ' A10 ' and ' dell '    Else ' any '    end) Machinenamefrom tablename  

 input_expression is generally a table field, or Other valid SQL expressions (this is not quite clear).

 The when_expression is the same as the input_expression data type, or can be implicitly converted. In other words, this is not a Boolean expression (string comparison, expression evaluation, etc.), unless input_expression is of type bool.

Complete Search case

case where    boolean_expression then result_expression        [... n]    [         ELSE else_result_expression    END

 When Boolean_expression=true, return to result_expression.

Such as

Select when Aix = ' s400 ' and ' hp ' when    aix = ' A10 ' and ' dell '    Else ' any ' end '    machinenamefrom ta Blename  

  It can be understood that when a case is omitted as a simple case, true. The implementation process or difference between the two has yet to be studied.

  

  

MySQL's if,case use notes

Related Article

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.