1. 程式人生 > >sql查詢一個表不包含另一個表的資料,兩種方法。

sql查詢一個表不包含另一個表的資料,兩種方法。

1、用minus函式的方式:

select 

from 
CMN_FUND
where 
FUND_CODE_ in 
(select FUND_CODE_ from CMN_FUND minus select FUND_CODE_ from CMN_PROMOTION_FUND where FUND_CODE_ in 
(select FUND_CODE_ from CMN_PROMOTION_FUND where SELLPROMOTE_CODE_ = #{promotionCode}))
and 
FUNDCOMP_CODE_ = #{fundCompCode} 
order by 

FUND_CODE_

2、用not in來處理:

select 
f.FUND_CODE_, f.SHORT_NAME
from
CMN_FUND f, CMN_PROMOTION_FUND p
where
f.FUND_CODE_ = p.FUND_CODE_
and
f.FUND_CODE_ not in (SELECT FUND_CODE_ from CMN_PROMOTION_FUND where PROMOTION_CODE = #{promotionCode})
and 
f.FUNDCOMPCODE = #{fundCompCode}