[英]Oracle SQL CASE expression in WHERE clause only when conditions are met
无法想象这一个,我有一套条件,只有当一个值在一个字段中时我才能满足。
因此,如果状态完成,我希望有三个where子句,如果状态不等于完成,那么我不想要任何where子句。
码
SELECT *
FROM mytable
WHERE CASE WHEN Status = 'Complete'
THEN (included = 0 OR excluded = 0 OR number IS NOT NULL)
ELSE *Do nothing*
通常在WHERE
只使用布尔表达式很简单。 所以:
WHERE (Status <> 'Complete') OR
(included = 0 OR excluded = 0 OR number IS NOT NULL)
如果Status
可能为NULL
:
WHERE (Status <> 'Complete' OR Status IS NULL) OR
(included = 0 OR excluded = 0 OR number IS NOT NULL)
您可以在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;
它看起来不像你真的需要一个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.