How to use the 'IF' in a MySQL 'SELECT' statement? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to use the 'IF' in a MySQL 'SELECT' statement?

Damian99Damian99 USAPosts: 116Member

I need to use a IF condition check inside a SELECT statement of MySQL. Can someone please help?



  • DavidMDavidM USAPosts: 342Member

    From the official documentation,


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

    mysql> SELECT IF(1>2,2,3);
            -> 3
    mysql> SELECT IF(1<2,'yes','no');
            -> 'yes'
    mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
            -> 'no'
Sign In or Register to comment.