soql – Query to find objects where field values match on each

I’m trying to write a query that finds all objects where a field on each object has the same value.
The value is the id of another object, so I don’t know what it is, I just know it will be the same on each object

object1__c : {
    id: 'id1',
    field__c: object2__c.id
}
object1__c : {
    id: 'id2',
    field__c: object3__c.id
}
object1__c : {
    id: 'id3',
    field__c: object2__c.id
}
object1__c : {
    id: 'id4',
    field__c: object2__c.id
}

So the I want to return:

[object1__c : {
    id: 'id1',
    field__c: object2__c.id
}
object1__c : {
    id: 'id3',
    field__c: object2__c.id
}
object1__c : {
    id: 'id4',
    field__c: object2__c.id
}]

I’m honestly a little lost at what to do after SELECT id, field__c FROM object1__c which obviously gives me everything.

Is there a way of looping over the list and picking out objects where the fields match?

Read more here: Source link