简体   繁体   English

在WHERE子句中添加CASE WHEN

[英]Adding CASE WHEN to WHERE clause

I used the following WHERE clause 我使用了以下WHERE子句

WHERE (TYPE1 = :P3_ITEM1 OR :P3_ITEM1 IS NULL)

to filter my query on TYPE1 and it worked fine to display the records of same TYPE1 when page item P3_ITEM1 is not null. 来过滤我对TYPE1的查询,当页面项P3_ITEM1不为null时,可以很好地显示相同TYPE1的记录。

Now I need to add two more filters for TYPE2 and TYPE3. 现在,我需要为TYPE2和TYPE3添加两个过滤器。 Normally only one page item P3_ITEM1, P3_ITEM2, or P3_ITEM3 is not null, so I need to filter on whichever on is not null. 通常只有一个页面项目P3_ITEM1,P3_ITEM2或P3_ITEM3不为空,因此我需要对不为空的任何一个进行过滤。

I tried 我试过了

WHERE (TYPE1=:P3_ITEM1 OR :P3_ITEM1 IS NULL) OR
  (TYPE2=:P3_ITEM2 OR :P3_ITEM2 IS NULL) OR
  (TYPE3=:P3_ITEM3 OR :P3_ITEM3 IS NULL) 

but it did not work. 但它没有用。

I tried using CASE statement in my WHERE clause but no success so far. 我尝试在WHERE子句中使用CASE语句,但到目前为止没有成功。 Can anyone help? 有人可以帮忙吗?

WHERE CASE
WHEN (:P3_ITEM1 IS NOT NULL) THEN (TYPE1=:P3_ITEM1)
WHEN (:P3_ITEM2 IS NOT NULL) THEN (TYPE2=:P3_ITEM2)
WHEN (:P3_ITEM3 IS NOT NULL) THEN (TYPE3=:P3_ITEM3)

You can simply use AND to combine the various filters. 您可以简单地使用AND组合各种过滤器。 Each filter will evaluate to true if the parameter for it is NULL , so if all three are NULL you will get all records, but you can fill in a any, or even all three parameters to filter out unwanted records. 如果每个过滤器的参数为NULL ,则其评估结果为true,因此,如果所有三个参数均为NULL ,则将获取所有记录,但您可以填写any甚至所有三个参数以过滤掉不需要的记录。

WHERE
  (TYPE1 = :P3_ITEM1 OR :P3_ITEM1 IS NULL) AND
  (TYPE2 = :P3_ITEM2 OR :P3_ITEM2 IS NULL) AND
  (TYPE3 = :P3_ITEM3 OR :P3_ITEM3 IS NULL)

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

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