[英]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.