0

私は以下のようなクエリを持っています

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

私はしたいと思います

(a.column='123') が null でない場合にのみ、「(c.column='456')」を含めます

単一のクエリでそれを行うにはどうすればよいですか? または、2 つの別個のクエリを作成する必要がありますか?

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.columnnull の場合、またはボットa.columnc.column有効な値がある場合は、行が含まれます。

于 2013-07-29T15:33:09.183 に答える