从子句限制SOQL查询结果

时间:2015-08-21 17:26:57

标签: salesforce apex-code apex 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记录的product2记录?

所以假设我有10个产品,但只有两个匹配WHERE子句。我只希望这两个出现,所以我不浪费资源/行。

2 个答案:

答案 0 :(得分: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 :(得分:1)

你可以像Daniel的评论中所提到的那样反向进行查询,或者尝试将条件放在顶部查询中

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)