[英]case when clause in sql query in db2
I have a db2/AS400 database and I'm trying to do the following query:我有一个 db2/AS400 数据库,我正在尝试执行以下查询:
SELECT *
FROM user
WHERE id IN (
CASE
WHEN 1=1 THEN
(SELECT id
FROM user AS userInner
WHERE userInner.salary < 10)
ELSE
(SELECT id
FROM user AS userInner
WHERE userInner.salary > 20)
END
)
The 1=1 condition depends on something else, but it's ok for this simple example. 1=1 条件取决于其他因素,但对于这个简单的例子来说是可以的。 However what I get as an error is the following:
但是我得到的错误如下:
SQL Error [21000]: [SQL0811] Result of SELECT more than one row.
SQL 错误 [21000]:[SQL0811] SELECT 多于一行的结果。
How it would be possible to use case when or other way in order to have a sql_statement condition with multiple results?如何使用 case when 或其他方式来获得具有多个结果的 sql_statement 条件?
Thanks谢谢
I don't think you need CASE
.我认为你不需要
CASE
。
You can do it like this:你可以这样做:
SELECT *
FROM user
WHERE id IN (
SELECT id FROM user
WHERE (1=1 AND userInner.salary < 10)
OR (1<>1 AND userInner.salary > 20)
)
Of course you have to replace 1=1
with your actual condition and 1<>1
with its reverse.当然,您必须将
1=1
替换为您的实际情况,并将1<>1
替换为相反。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.