0

我有一个查询如下

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

我想

仅当 (a.column='123') 不为空时才包含 "(c.column='456')"

我如何在单个查询中做到这一点?还是我需要编写两个单独的查询?

4

2 回答 2

0

据我了解您的要求,这是您想要的 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。无法测试它。

于 2013-07-29T16:48:48.730 回答
0

应该很简单:

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如果它为 null 或 bota.column并且c.column具有有效值,它将包含一行。

于 2013-07-29T15:33:09.183 回答