Oracle 11g SQL where 子句中的 case 语句

Oracle 11g SQL case statement in where clause

我正在尝试修改查询中的 where 子句,因为它在 Oracle 报告的国家 table 上创建笛卡尔连接。问题行如下。报告变量 :P_PAYMENT_SOC_NBR 可以是 NULL、'052' 或 '044',如果 :P_PAYMENT_SOC_NBR 是 NULL 那么我想同时搜索 member.d_tax_country_id、member.d_mcps_tax_country_id country_id 国家 table。

我正在考虑在 where 子句中使用 case 语句,但我卡住了。

 AND country.country_id = 
 decode(:P_PAYMENT_SOC_NBR, NULL,country.country_id,'052',
        member.d_tax_country_id,member.d_mcps_tax_country_id)   

关于做什么的任何想法

   AND ((:P_PAYMENT_SOC_NBR = '052' and country.country_id = member.d_tax_country_id)
     OR (:P_PAYMENT_SOC_NBR = '044' and country.country_id = member.d_mcps_tax_country_id)
     OR (:P_PAYMENT_SOC_NBR is null and (country.country_id = member.d_tax_country_id OR country.country_id = member.d_mcps_tax_country_id)))

您的要求不是很明确。如果 :P_PAYMENT_SOC_NBR 而不是 NULL,会发生什么?从您的文字描述来看,在这种情况下您似乎不需要检查任何进一步的条件。如果是这样使用:

AND (:P_PAYMENT_SOC_NBR is not NULL 
     or country.country_id = member.d_tax_country_id
     or country.country_id = member.d_mcps_tax_country_id  -- or perhaps "and", not "or"??
    )

如果这不是您的意思,请用简单的英语说明您的要求(很明显,查询不符合您的要求不会有太大帮助)。