Sql server 2005 内部联接中的SQL查询问题
我有表Sql server 2005 内部联接中的SQL查询问题,sql-server-2005,Sql Server 2005,我有表Creditcard,Childcreditcard1,Childcreditcard2 儿童信用卡1 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 456771321
Creditcard
,Childcreditcard1
,Childcreditcard2
儿童信用卡1
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
儿童信用卡2
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
万事达信用卡
CreditcardID primaryCreditID
22222132132 22222132132
22222132134 22222132132
12364132122 12364132122
45677132124 45677132124
45677232124 45677232124
78567723212 78567723212
23677232124 23677232124
45678944343 45678944343
22222732134 22222732134
12364132192 12364132192
现在,从这三个表中,我需要得到creditcard
,它不仅与MasterCreditcard
中的creditcard
中的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
选择
独特(抄送信用卡)
从…起
万事达信用卡
内连接
CC.CreditcardID c1.CreditcardID上的Childcreditcard1 c1
内连接
CC.CreditCardC2.CreditCardC2.CreditcardID上的Childcreditcard2 c2
我试过这样做,但这给出了
MasterCreditcard
表中所有creditcard
的结果,这是错误的试试看,这可能对您有用
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
我的答案是基于这张图片,第二张图片是左外角的盒子
答案对您有帮助吗?@happysmile-可以说这是什么吗?从MasterCreditcard中选择distinct(cc.CreditcardID)cc left outer JOIN Childcreditcard1 c1 ON cc.CreditcardID=c1.CreditcardID left outer JOIN Childcreditcard2 c2 ON cc.CreditcardID=c2.CreditcardID其中c1.CreditcardID为空,c2.CreditcardID为空