Sql 基于另一个条件动态包含一个条件

Sql 基于另一个条件动态包含一个条件,sql,oracle,Sql,Oracle,我有一个问题如下 select -------- from table a left outer join ....c where (a.column='123') and (c.column='456') 我想 仅当(a.column='123')不为空时才包括“(c.column='456')” 如何在单个查询中实现这一点?或者我需要编写两个单独的查询吗?应该非常简单: select -------- from table left outer join.... where (Condi

我有一个问题如下

select --------
from table a
left outer join ....c
where 
(a.column='123') and (c.column='456')
我想

仅当(a.column='123')不为空时才包括“(c.column='456')”


如何在单个查询中实现这一点?或者我需要编写两个单独的查询吗?

应该非常简单:

select --------
from table
left outer join....
where (Condition A IS NULL) OR (condition A AND condition B)
更新:针对您的情况:

where (a.column is null) or (a.column='123' and c.column='456')

如果
a.column
为空,或者bot
a.column
c.column
具有有效值,则它将包含a行。

据我所知,这是您需要的sql

select distinct cm.credit_amt,e.credit_lifetime,e.credit_desc,e.icom_code,e.entry_hint,
                e.credit_id,e.credit_type_id,e.recontract_period,a.class_desc,a.offer_id,
                a.offer_class_id 
from rti_retention.package a 
left outer join rti_retention.offer_class_credit b on (a.offer_id=b.offer_id 
                                                       and a.offer_class_id=b.offer_class_id 
                                                       and a.customer_type_id=b.customer_type_id) 
left outer join rti_retention.credit_pre_bundle c on (b.credit_id=c.credit_id) 
left outer join rti_retention.credit e on (c.credit_id=e.credit_id) 
left outer join rti_retention.credit_mix_amount cm on (cm.credit_id=c.credit_id and cm.prod_mix_id=a.prod_mix_id)
where a.offer_class_id not in (1,2,16)
and a.channel_id=5 and a.customer_type_id=1 
and a.offer_id='6055' 
and c.prod_mix_id = case when (select count(*) 
                               from rti_retention.credit_pre_bundle c1 
                               where c1.prod_mix_id='1000' ) > 1 then '1000' else c.prod_mix_id end
and e.icom_code is not null

有时会出现一些sql语法错误。由于我没有完整的数据库,所以我考虑编写sql。我无法测试它

您正在使用什么rdbms?我正在使用的rdbms是oracleis a.column可为空的吗?很抱歉,即使是您最新(第三)版的问题,也不清楚您想做什么。我的两个条件是(a.column='123')和(c.column='456'))我的问题不是只有这两个条件,而是有其他条件以及针对您的条件的更新答案。其他人也应该如此。谢谢你的回复…我的要求错了…对不起