【发布时间】:2012-08-24 21:38:27
【问题描述】:
我想检索带有WHERE 子句中条件的数据。
这是我的表格:
Name Location Age
----------------------
AAA Bhuj 24
BBB Mumbai 22
CCC Bhuj 18
DDD Bhuj 27
EEE Mumbai 26
我在WHERE 子句中的条件是:if location = 'Bhuj' then Age>20 else if location = 'Mumbai' then Age>25
我正在尝试使用此代码来实现此目的:
SELECT * FROM testing
WHERE
CASE Location WHEN 'Bhuj' THEN Age>20
WHEN 'Mumbai' THEN Age>25
END;
此代码适用于 MySQL (see this SQLFiddle),但不适用于 SQL Server (see this SQLFiddle),并出现以下错误:
'>' 附近的语法不正确。:SELECT * FROM testing WHERE case Location When 'Bhuj' then Age>20 When 'Mumbai' then Age>25 end
有什么建议吗?
【问题讨论】:
-
看不到任何反对该问题的理由。赞成:)
-
@NitinGoyal 我的实际查询不同且复杂。我必须使用
CASE声明的地方。我在这里简单地展示了它。
标签: sql sql-server case where-clause multiple-conditions