0

I have a "table" Campaign Member in the Salesforce, with 2 records:

enter image description here

And I have a "table" Asset with 2 records too:

enter image description here

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?

1 Answer 1

5

It might not work with CampaignMember, but the mechanisms you need for this are called table joins and anti-joins. It would look something like:

SELECT Id FROM Account WHERE Id IN (SELECT AccountId FROM Asset) AND Id NOT IN (SELECT AccountId FROM CampaignMember) 
6
  • Should this be using related list relationship names (IIRC typically plural for standard object relationships) in the nested SELECTs? Commented Aug 10, 2022 at 21:19
  • @PhilW No, they operate like a top-level query. Commented Aug 11, 2022 at 0:02
  • You only use relationship names for joins in the SELECT clause. Commented Aug 11, 2022 at 0:02
  • 1
    I found it. The terms seem to be "semi-join" and "anti-join". Nice. I learned something new today :) Commented Aug 11, 2022 at 9:37
  • 2
    @PhilW If you haven't seen the old docs on joins, I scraped them for preservation and posted them here. Commented Aug 11, 2022 at 12:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.