function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arthur Almeida 12Arthur Almeida 12 

how to select all AccountId of the table X that is not in the table Y?

I have a "table" Campaign Member in the Salesforce, with 2 records:
User-added image
And I have a "table" Asset with 2 records too:
User-added image
I would like to know if there is a way to select all AccountId of the table Asset that is not in the table Campaign Member.
 
For example, the query would return just the AccountId 111, because Account Id 870 there are in the table of Campaign Member.
 
Exist some way to make this?
Best Answer chosen by Arthur Almeida 12
Arthur Almeida 12Arthur Almeida 12
thank you for your answer, this solution helped me:
SELECT Id FROM Account WHERE Id IN (SELECT AccountId FROM Asset) AND Id NOT IN (SELECT AccountId FROM CampaignMember)

 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Arthur,

try with below code in your deloper console.
 
List<CampaignMember > campmemberlist = [select id, AccountId from CampaignMember ];

set<id> accids = new set<id>();

for(CampaignMember  cm:campmemberlist ){

accids.add(cm.AccountId);

}

List<Asset> assetlist = [select id,AccountId from asset where AccountId !=:accids ];

system.debug('assetlist=='+assetlist);

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 
Arthur Almeida 12Arthur Almeida 12
thank you for your answer, this solution helped me:
SELECT Id FROM Account WHERE Id IN (SELECT AccountId FROM Asset) AND Id NOT IN (SELECT AccountId FROM CampaignMember)

 
This was selected as the best answer