IF(test_expression,expression_true,expression_false)

Description: Returns expression_true if test_expression is TRUE, or expression_false otherwise.

expression_test is interpreted as an integer. IF() returns a datatype according to the following:

  • If expression_true or expression_false returns a string, the result will be a string.

  • If expression_true or expression_false returns a floating-point number, the result will be a floating-point number.

  • If expression_true or expression_false returns an integer, the result will be an integer.

Examples:

  • IF(5>3,'more','not more'); returns more.

  • IF(2>3,'more',0) returns 0 (as a string).

  • IF(2>3,1,0) returns 0 (as a numeric).

Note that

  • IF(0.2,'nonzero','zero') returns zero (because 0.2 is converted to an integer, ...

Get Sams Teach Yourself MySQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.