【发布时间】:2016-11-23 08:40:06
【问题描述】:
我想根据查询结果选择where子句中的变量。
Select table1.*, table2.color, table3.type
from table1
inner join table2 on table1.ID=table2.table1Id
inner join table3 on table1.ID=table3.table1Id
where table3.type = @x OR table3.type = @y
| productName | Category | color | type |
| abc | electronics | blue | x |
| abc | electronics | blue | y |
| def | electronics | red | x |
这个查询可以返回重复的结果,因为产品可以有两种类型。我想在 where 子句中选择变量。例如,我想获取类型为@y 的产品,但如果产品的@y 类型不存在,我想返回@x 类型。我不希望示例结果中的第一个 abc 行。您能帮我解答一下我的问题吗?
【问题讨论】:
标签: sql sql-server where-clause