我如何IF...THEN在SQL SELECT声明中执行?
例如:
SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
Run Code Online (Sandbox Code Playgroud) 我需要根据某种优先级从表中选择数据,如下所示:
select product, price from table1 where project = 1
-- pseudo: if no price found, do this:
select product, price from table1 where customer = 2
-- pseudo: if still no price found, do this:
select product, price from table1 where company = 3
Run Code Online (Sandbox Code Playgroud)
也就是说,如果我找到3个基于价格的产品project = X,我不想选择customer = Y.我只想返回结果3行并完成.
你怎么在SQL中做这样的事情?对pseudo-if使用某种CASE语句?做工会还是其他聪明的事情?
编辑:我正在使用MS SQL.
谢谢!