2

I have two custom objects in Salesforce: Object1 and Object2 Object2 has a lookup field that references to Object1. More than one record in Object2 could have the same Object1 record referenced. I have to build a SOQL query wich makes a join of Object1 and Object2 where the matching is one-to-one.

ie. With those values in Object1 and Object2 I want that result:

With those values in Object1 and Object2 I want that result

The record in Object1 with Id=2 is not in result because it has two records in Object2 that references it. I would like to know how to achieve this with a SOQL query.

Thanks in advance!

4

1 回答 1

4

因为您只想要一行,您可以偷偷地使用聚合函数从聚合查询中获取值。

select max(id) object2Id, 
       max(name) object2Val, 
       max(object1__r.name) object1Val 
from object2__c 
group by object1__c 
having count(object1__c) =1

为我工作(在我的对象中使用名称字段而不是值,但这无关紧要)。

样本查询结果

于 2012-07-17T18:02:31.457 回答