2015-08-21 200 views
1

限制SOQL查询结果希望这将是很容易回答:从孩子条款

select name, id, description, isactive, productcode, imageurl__c, (select name, id, unitprice,Must_Override_Price__c, Is_Taxable__c from PricebookEntries where pricebook2id =: pbe.id) from product2 

如何防止有被质疑没有孩子pricebookentry记录产品2的记录?

因此可以说我有10个产品,但只有两个匹配WHERE子句。我只想让这两个人出现,所以我不会浪费资源/行。

回答

1

反向执行查询关系。您仍然可以获取所有必需的Product2字段,并且您只会获得现有PricebookEntry记录的结果。

E.g.

select Id, Name, Pricebook2Id, Product2Id, UnitPrice, IsActive, UseStandardPrice, 
     ProductCode, IsDeleted, 
     Product2.Id, Product2.Name 
from PricebookEntry 
where pricebook2id = :pbe.id 
1

作为丹尼尔的评论中提及或尝试把你可以做反向查询的WHERE条件顶层查询以及

select name, id, description, isactive, productcode, imageurl__c, 
    (select name, id, unitprice,Must_Override_Price__c, Is_Taxable__c 
    from PricebookEntries 
    where pricebook2id =: pbe.id) 
from product2 
where Id IN (select Product2Id 
       from PricebookEntries 
      where pricebook2id =: pbe.id)