MYSQL if statement question

前端 未结 2 1443
借酒劲吻你
借酒劲吻你 2021-01-23 11:56
Is there any way to get the if statement to evaluate a query?

SELECT if(5>0,\'EQ_Type\',\'*\') FROM EQUIPMENT;

Resulting in:
+-----------------------+
| IF(5>0,\'EQ_Type\',         


        
相关标签:
2条回答
  • 2021-01-23 12:07

    I recommend using ANSI notation - CASE statements in this case - rather than MySQL specific syntax:

    SELECT CASE WHEN 5 > 0 THEN t.eq_type ELSE '*' END
      FROM EQUIPMENT t
    

    Reason being, if you changed databases - this statement wouldn't need to be changed to work on Oracle, Postgres, SQL Server, etc.

    0 讨论(0)
  • 2021-01-23 12:17

    Just remove the single quotes.

    SELECT if(5>0,EQ_Type,'*') FROM EQUIPMENT;
    
    0 讨论(0)
提交回复
热议问题