【发布时间】:2013-02-11 17:15:57
【问题描述】:
我需要根据某种优先级从表中选择数据,如下所示:
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
也就是说,如果我发现 3 个产品的价格基于 project = X,我不想在 customer = Y 上进行选择。我只想返回结果 3 行并完成。
你应该如何在 SQL 中做这样的事情?对伪 if 使用某种 CASE 语句?做一个工会或其他一些聪明的事情?
编辑:我正在使用 MS SQL。
谢谢!
【问题讨论】:
-
您使用的是哪种 RDBMS,MS SQL Server、MySQL、PostgreSQL...?对于它们中的每一个,控制流指令集都有些不同
-
您回答了自己的问题!用例语句!
-
从 SQL 中查看 CASE 语句
标签: sql sql-server