简体   繁体   English

选择表A中不在表B中的列组合

[英]Select Combination of columns from Table A not in Table B

I have two sets of table with all the contacts on an Account their titles and etc. For data migration purposes I need to select All ContactsIds with their AccountID from Table A that do not exist in TableB. 我有两组表,帐户上的所有联系人都有他们的标题等。出于数据迁移的目的,我需要从表A中选择所有ContactsIds和表ID中不存在的AccountID。 Its the combination of both the ContactId and the AccountID. 它是ContactId和AccountID的组合。 I have tried the following: 我尝试过以下方法:

Select * from Final_Combined_Result wfcr 

 WHERE NOT EXISTS (Select Contact_ID, Account_ID from Temp_WFCR)  

I know this is completely off, but I have looked at a couple of other questions on here but was unable to find an appropriate solution. 我知道这完全是关闭的,但我在这里看了几个其他的问题,但无法找到合适的解决方案。

I have also tried this: 我也试过这个:

Select * from Final_Combined_Result wfcr  
WHERE NOT EXISTS 
  (Select Contact_ID, Account_ID from Temp_WFCR as tc 
   where tc.Account_ID=wfcr.Account_InternalID 
   AND tc.Account_ID=wfcr.Contact_InternalID)

This seems to be correct but I would like to make sure. 这似乎是正确的,但我想确定。

Select wfcr.ContactsId, wfcr.AccountID
from Final_Combined_Result wfcr 
left join Temp_WFCR t_wfcr ON t_wfcr.ContactsIds = wfcr.ContactsId 
                          AND t_wfcr.AccountID = wfcr.AccountID
WHERE t_wfcr.AccountID is null 

See this great explanation of joins 看到关于连接的这个很好的解释

@juergend's answer shows the left join. @ juergend的答案显示了左连接。

Using a not exists you join in the subselect, it would look like this: 使用a not exists加入subselect,它看起来像这样:

Select wfcr.*
from 
  Final_Combined_Result wfcr 
  WHERE NOT EXISTS 
     (Select 1 --select values dont matter here, only the join restricts.
     from 
       Temp_WFCR t
     where t.Contact_ID = wfcr.Contact_InternalID
       and t.account_id = wfcr.Account_InternalID 
     ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM