12.2. Control Flow Functions

MySQL 5.0

12.2. Control Flow Functions

  • WHEN [] THEN [WHEN [] THEN ...] [ELSE ] END

    ] THEN [WHEN [] THEN ...] [ELSE ] END

    The first version returns the where =. The second version returns the result for the first condition that is true. If there was no matching result value, the result after is returned, or if there is no part.

    mysql> 
        ->     
            -> 'one'
    mysql> 
            -> 'true'
    mysql> 
        ->     
            -> NULL
    

    The default return type of a expression is the compatible aggregated type of all return values, but also depends on the context in which it is used. If used in a string context, the result is returned as a string. If used in a numeric context, then the result is returned as a decimal, real, or integer value.

    Note: The syntax of the expression shown here differs slightly from that of the SQL statement described in Section 17.2.10.2, “ Statement”, for use inside stored routines. The statement cannot have an clause, and it is terminated with instead of .

  • ,,)

    If is ( <> 0 and <> NULL) then returns ; otherwise it returns . returns a numeric or string value, depending on the context in which it is used.

    mysql> 
            -> 3
    mysql> 
            -> 'yes'
    mysql> 
            -> 'no'
    

    If only one of or is explicitly , the result type of the function is the type of the non- expression.

    is evaluated as an integer value, which means that if you are testing floating-point or string values, you should do so using a comparison operation.

    mysql> 
            -> 0
    mysql> 
            -> 1
    

    In the first case shown, returns because is converted to an integer value, resulting in a test of . This may not be what you expect. In the second case, the comparison tests the original floating-point value to see whether it is non-zero. The result of the comparison is used as an integer.

    The default return type of (which may matter when it is stored into a temporary table) is calculated as follows:

    Expression Return Value
    or returns a string string
    or returns a floating-point value floating-point
    or returns an integer integer

    If and are both strings, the result is case sensitive if either string is case sensitive.

    Note: There is also an statement, which differs from the function described here. See Section 17.2.10.1, “ Statement”.

  • ,)

    If is not , returns ; otherwise it returns . returns a numeric or string value, depending on the context in which it is used.

    mysql> 
            -> 1
    mysql> 
            -> 10
    mysql> 
            -> 10
    mysql> 
            -> 'yes'
    

    The default result value of ,) is the more “general” of the two expressions, in the order , , or . Consider the case of a table based on expressions or where MySQL must internally store a value returned by in a temporary table:

    mysql> 
    mysql> 
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | test  | char(4) |      |     |         |       |
    +-------+---------+------+-----+---------+-------+
    

    In this example, the type of the column is .

  • ,)

    Returns if = is true, otherwise returns . This is the same as = THEN NULL ELSE END.

    mysql> 
            -> NULL
    mysql> 
            -> 1
    

    Note that MySQL evaluates twice if the arguments are not equal.