说我必须遵循:
Select OrderID =
Case OrderID
When 1 Then 'Customer1'
When 2 Then 'Customer2'
When 3 Then 'Customer2'
Else 'Unknown Customer'
End
From OrdersPlaced
是否可以添加或执行以下操作:
Select OrderID =
Case OrderID
When 1 Then 'Customer1'
When 2 Or 3 Then 'Customer2'
Else 'Unknown Customer'
End
From OrdersPlaced
答案 0 :(得分:10)
CASE
表达有两种形式,'搜索'和'简单'。您不能将OR
与“简单”CASE
表达式一起使用,但您可以使用“已搜索”表单:
Case
When OrderID = 1 Then 'Customer1'
When OrderID = 2 Or
OrderID = 3 Then 'Customer2'
Else 'Unknown Customer'
End
甚至
Case
When OrderID = 1 Then 'Customer1'
When OrderID IN (2, 3) Then 'Customer2'
Else 'Unknown Customer'
End
答案 1 :(得分:2)
您可以使用CASE的替代形式
Select OrderID =
Case
When OrderID = 1 Then 'Customer1'
When OrderID = 2 Or OrderID = 3 Then 'Customer2'
Else 'Unknown Customer'
End
From OrdersPlaced
答案 2 :(得分:1)
CASE
表达式有两种语法 - 简单(即您正在显示的语法)和搜索的语法,具有多种逻辑条件。如果您想使用OR
,则需要第二种:
Select OrderID =
Case
When OrderID =1 Then 'Customer1'
When OrderID =2 Or OrderID =3 Then 'Customer2'
Else 'Unknown Customer'
End
From OrdersPlaced
以下是documentation:
的说明答案 3 :(得分:1)
试试这个。这适用于您的场景。
Select OrderID =
Case
When OrderID = 1 Then 'Customer1'
When OrderID in (2, 3) Then 'Customer2'
Else 'Unknown Customer'
End
From OrdersPlaced