“Case When Else In Oracle Sql” 可以翻译为 “Oracle SQL 中的 Case When Else”。

huangapple go评论62阅读模式
英文:

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 (&#39;31&#39;, &#39;21&#39;, &#39;51&#39;, &#39;45&#39;, &#39;35&#39;) OR
    lomht.ORDER_TRX_ID != :ORDER_TRX_ID AND
    typ.ID (&#39;12&#39;, &#39;13&#39;);

答案2

得分: 1

在SQL中,CASE表达式返回单个标量值(不是比较或值列表)。

您可以使用ANDOR逻辑运算符以及括号来确定优先级,需要确保检查等效的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 (&#39;31&#39;, &#39;21&#39;, &#39;51&#39;, &#39;45&#39;, &#39;35&#39;)
       )
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 (&#39;12&#39;, &#39;13&#39;)
       );

huangapple
  • 本文由 发表于 2023年7月18日 12:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定