英文:
Case When Else In Oracle Sql
问题
以下是翻译好的部分:
我有一个查询如下,但我收到以下错误:
SQL错误 [907] [42000]:ORA-00907:缺少右括号
代码:
SELECT
typ.ID ID, typ.DETAILS DETAILS
FROM
LMP_ORDER_MW_HOST_SVC_TYP typ
LEFT JOIN
LMP_ORDER_MW_HOST_TRX lomht ON typ.ID = lomht.ORDER_SVC_TYP_ID
WHERE
typ.ID IN (CASE
WHEN lomht.ORDER_TRX_ID = :ORDER_TRX_ID
THEN ('31', '21', '51', '45', '35')
ELSE ('12', '13')
END);
英文:
I have a query like below, but I got the following error::
> SQL Error [907] [42000]: ORA-00907: missing right parenthesis
Code:
SELECT
typ.ID ID, typ.DETAILS DETAILS
FROM
LMP_ORDER_MW_HOST_SVC_TYP typ
LEFT JOIN
LMP_ORDER_MW_HOST_TRX lomht ON typ.ID = lomht.ORDER_SVC_TYP_ID
WHERE
typ.ID IN (CASE
WHEN lomht.ORDER_TRX_ID = :ORDER_TRX_ID
THEN ('31', '21', '51', '45', '35')
ELSE ('12', '13')
END);
答案1
得分: 1
CASE
表达式实际上旨在生成新的值。相反,您应该在您的 WHERE
子句中只使用直接的和/或逻辑:
SELECT typ.ID ID, typ.DETAILS DETAILS
FROM LMP_ORDER_MW_HOST_SVC_TYP typ
LEFT JOIN LMP_ORDER_MW_HOST_TRX lomht
ON typ.ID = lomht.ORDER_SVC_TYP_ID
WHERE
lomht.ORDER_TRX_ID = :ORDER_TRX_ID AND
typ.ID IN ('31', '21', '51', '45', '35') OR
lomht.ORDER_TRX_ID != :ORDER_TRX_ID AND
typ.ID ('12', '13');
英文:
CASE
expressions are really intended to generate new values. Instead, you should just use straight and/or logic in your WHERE
clause:
<!-- language: sql -->
SELECT typ.ID ID, typ.DETAILS DETAILS
FROM LMP_ORDER_MW_HOST_SVC_TYP typ
LEFT JOIN LMP_ORDER_MW_HOST_TRX lomht
ON typ.ID = lomht.ORDER_SVC_TYP_ID
WHERE
lomht.ORDER_TRX_ID = :ORDER_TRX_ID AND
typ.ID IN ('31', '21', '51', '45', '35') OR
lomht.ORDER_TRX_ID != :ORDER_TRX_ID AND
typ.ID ('12', '13');
答案2
得分: 1
在SQL中,CASE
表达式返回单个标量值(不是比较或值列表)。
您可以使用AND
和OR
逻辑运算符以及括号来确定优先级,需要确保检查等效的ELSE
条件是否测试了NULL
值(因为在三元逻辑中,NULL
既不等于也不不等于其他任何值):
SELECT typ.ID,
typ.DETAILS
FROM LMP_ORDER_MW_HOST_SVC_TYP typ
LEFT OUTER JOIN LMP_ORDER_MW_HOST_TRX lomht
ON typ.ID = lomht.ORDER_SVC_TYP_ID
WHERE ( lomht.ORDER_TRX_ID = :ORDER_TRX_ID
AND typ.ID IN ('31', '21', '51', '45', '35')
)
OR ( ( lomht.ORDER_TRX_ID != :ORDER_TRX_ID
OR lomht.ORDER_TRX_ID IS NULL
OR :ORDER_TRX_ID IS NULL
)
AND typ.ID IN ('12', '13')
);
英文:
In SQL, a CASE
expression returns a single scalar value (not a comparison or a list of values).
Instead of using a CASE
expression, you can use the AND
and OR
logical operators and brackets for precedence and you need to make sure that you check the equivalent ELSE
condition tests for NULL
values (because, with trinary logic, NULL
is never equal nor not-equal to anything else):
SELECT typ.ID,
typ.DETAILS
FROM LMP_ORDER_MW_HOST_SVC_TYP typ
LEFT OUTER JOIN LMP_ORDER_MW_HOST_TRX lomht
ON typ.ID = lomht.ORDER_SVC_TYP_ID
WHERE ( lomht.ORDER_TRX_ID = :ORDER_TRX_ID
AND typ.ID IN ('31', '21', '51', '45', '35')
)
OR ( ( lomht.ORDER_TRX_ID != :ORDER_TRX_ID
OR lomht.ORDER_TRX_ID IS NULL
OR :ORDER_TRX_ID IS NULL
)
AND typ.ID IN ('12', '13')
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论