简体   繁体   English

Where子句中的Oracle SQL Case语句

[英]Oracle SQL Case Statement in Where Clause

I'm new to Oacle SQL and I'm trying to get my head around what seems to be something easy... 我是Oacle SQL的新手,我试图弄清似乎很容易的事情...

Say I have a select like so: 说我有这样的选择:

select * from MRCONTRACT2
WHERE CASE 
        WHEN ("MR_CONTRACT2"."TERM_DATE" < sysdate)
            THEN 'Lapsed Contract'
        WHEN ("MR_CONTRACT2"."EFF_DATE" > sysdate)
            THEN 'Inactive Contract'
        ELSE 'Active Contract'
        END = 'Active Contract'

What does the where clause actually mean, as to me it does not make a full 'if' expression? where子句实际上是什么意思,对我而言,它没有完整的'if'表达式?

That query is equivalent to 该查询相当于

select *
  from mrcontract2
 where term_date >= sysdate
   and eff_date <= sysdate

The CASE statement is evaluated in order, so if any "case" is true then every case after it is ignored. CASE语句按顺序求值,因此,如果任何“ case”为true,则忽略其后的每个case。 As you're checking whether the result of the case statement is the same as the ELSE clause then the statement is the same as the opposite of all other conditions. 当您检查case语句的结果是否与ELSE子句相同时,则该语句与所有其他条件的相反。

I don't really like CASE statements in the WHERE clause but it can be useful as a way of simplifying logic; 我不太喜欢WHERE子句中的CASE语句,但是它可以用作简化逻辑的一种方式。 having a complex CASE statement that you want to evaluate against but don't want to translate. 您想要评估但不想翻译的复杂CASE语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM