简体   繁体   English

仅当满足条件时,WHERE子句中的Oracle SQL CASE表达式

[英]Oracle SQL CASE expression in WHERE clause only when conditions are met

Cant quite figure this one out, i have a set of conditions that i want to be met only if a value is in a field. 无法想象这一个,我有一套条件,只有当一个值在一个字段中时我才能满足。

So if the Status is complete i want to have three where clause's, if the status doesn't equal complete then i don't want any where clause. 因此,如果状态完成,我希望有三个where子句,如果状态不等于完成,那么我不想要任何where子句。

Code

SELECT *
FROM mytable
WHERE CASE WHEN Status = 'Complete'
           THEN (included = 0 OR excluded = 0 OR number IS NOT NULL)
           ELSE *Do nothing*

It is usually simple to only use boolean expressions in the WHERE . 通常在WHERE只使用布尔表达式很简单。 So: 所以:

WHERE (Status <> 'Complete') OR 
      (included = 0 OR excluded = 0 OR number IS NOT NULL)

If Status could be NULL : 如果Status可能为NULL

WHERE (Status <> 'Complete' OR Status IS NULL) OR 
      (included = 0 OR excluded = 0 OR number IS NOT NULL)

You can translate the natural language in SQL, then, if possible, reformulate. 您可以在SQL中翻译自然语言,然后,如果可能,重新制定。

SELECT *
FROM mytable
WHERE (Status = 'Complete' and (included = 0 OR excluded = 0 OR number IS NOT NULL)) 
     or status <> 'Complete'
     or status IS NULL;

It doesn't look like you really need a CASE statement, just use it like this: 它看起来不像你真的需要一个CASE语句,只需像这样使用它:

SELECT *
FROM mytable
WHERE where (Status = 'Complete' and (included = 0 OR excluded = 0 OR number IS NOT NULL)) or (*Your do nothing*)

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

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