内部联接中的SQL查询问题

时间:2012-04-11 12:48:17

标签: sql-server-2005

我有表CreditcardChildcreditcard1Childcreditcard2

Childcreditcard1

CreditcardID  TranscationDatetime
22222132132   2010-04-11 12:36:10.210
22222132134   2011-04-11 12:36:10.210
12364132122   2019-04-11 12:36:10.210
45677132124   2011-04-11 12:36:10.210
45677132124   2012-04-11 12:36:10.210

Childcreditcard2

CreditcardID  TranscationDatetime
22222132132   2010-04-11 12:36:10.210
22222732134   2011-04-11 12:36:10.210
12364132192   2019-04-11 12:36:10.210

MasterCreditcard

CreditcardID            primaryCreditID
22222132132     22222132132             
22222132134     22222132132             
12364132122     12364132122         
45677132124     45677132124
45677232124     45677232124     
78567723212     78567723212 
23677232124     23677232124 
45678944343     45678944343
22222732134     22222732134
12364132192              12364132192

现在从这三个表格中,我需要得到的creditcardID只与MasterCreditcard CreditcardID Childcreditcard1 Childcreditcard2 MasterCreditcard中的 45677232124 78567723212 23677232124 45678944343 不匹配将永远存在于select distinct(cc.CreditcardID) from MasterCreditcard CC inner JOIN Childcreditcard1 c1 ON CC.CreditcardID <> c1.CreditcardID inner JOIN Childcreditcard2 c2 ON CC.CreditcardID <> c2.CreditcardID

结果应该是表格应该是这样的

CreditcardID

我的查询:

MasterCreditcard

我尝试了这样但是这会给{{1}}表中所有{{1}}的结果带来错误

1 个答案:

答案 0 :(得分:2)

试试这可能对你有用

select distinct(cc.CreditcardID) from MasterCreditcard  CC  
left JOIN Childcreditcard1 c1 ON CC.CreditcardID =c1.CreditcardID  
left JOIN Childcreditcard2 c2 ON CC.CreditcardID =c2.CreditcardID   
where c1.CreditcardID  is null or c2.CreditcardID   is null

我的回答是基于这张图片,第二张图片是左外壳案例

alt text