How do I perform an IF…THEN in an SQL SELECT?

前端 未结 30 1867
梦如初夏
梦如初夏 2020-11-21 22:50

How do I perform an IF...THEN in an SQL SELECT statement?

For example:

SELECT IF(Obsolete = \'N\' OR InStock = \'Y\' ? 1 :          


        
30条回答
  •  梦毁少年i
    2020-11-21 23:26

    I like the use of the CASE statements but the question asked for an IF statement in the SQL Select. What I've used in the past has been:

    SELECT
    
       if(GENDER = "M","Male","Female") as Gender
    
    FROM ...
    

    It's like the excel or sheets IF statements where there is a conditional followed by the true condition and then the false condition:

    if(condition, true, false)
    

    Furthermore, you can nest the if statements (but then use should use a CASE :-)

    (Note: this works in MySQLWorkbench but may not work in other platforms)

提交回复
热议问题