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

前端 未结 30 1842
梦如初夏
梦如初夏 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条回答
  •  鱼传尺愫
    2020-11-21 23:27

    Question:

    SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
    

    ANSI:

    Select 
      case when p.Obsolete = 'N' 
      or p.InStock = 'Y' then 1 else 0 end as Saleable, 
      p.* 
    FROM 
      Product p;
    

    Using aliases -- p in this case -- will help prevent issues.

提交回复
热议问题