MYSQL if statement question

前端 未结 2 1446
借酒劲吻你
借酒劲吻你 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.

提交回复
热议问题