如何用Fetch查询筛选所有关联电话均非开启状态的账户?
如何用Fetch查询获取所有关联电话均为非开启状态的账户
当然可以用Fetch实现这个需求!你当前的查询是找出至少存在一个非开启状态电话的账户,但这会包含那些同时拥有开启和非开启电话的账户——显然这不是你想要的结果。要获取所有关联电话都处于非开启状态(已取消/已完成),甚至没有任何关联电话的账户,我们需要调整查询逻辑,通过not exists来排除那些存在开启状态电话的账户。
方案1:包含无关联电话的账户
如果你想把没有任何电话记录的账户也包含进来,可以用这个查询:
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='account'> <attribute name='name' /> <order attribute='accountamount' descending='true' /> <filter type='and'> <!-- 排除所有存在开启状态电话的账户 --> <not exists> <link-entity name='phonecall' from='regardingobjectid' to='accountid' alias='ab'> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> </filter> </link-entity> </not exists> </filter> </entity> </fetch>
方案2:仅包含有至少一个关联电话且全部非开启的账户
如果你只想保留那些确实有电话记录,但所有电话都处于非开启状态的账户,就需要额外添加一个exists条件,确保账户至少关联了一个电话:
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='account'> <attribute name='name' /> <order attribute='accountamount' descending='true' /> <filter type='and'> <!-- 确保账户至少有一个关联电话 --> <exists> <link-entity name='phonecall' from='regardingobjectid' to='accountid' alias='ab' /> </exists> <!-- 排除所有存在开启状态电话的账户 --> <not exists> <link-entity name='phonecall' from='regardingobjectid' to='accountid' alias='ac'> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> </filter> </link-entity> </not exists> </filter> </entity> </fetch>
逻辑说明
核心思路是反向排除:我们不直接找所有电话都非开启的账户,而是排除掉那些拥有任何开启状态电话的账户。剩下的账户自然就满足“所有关联电话都是非开启状态”(或者没有电话)的要求。
内容的提问来源于stack exchange,提问作者Damkulul
相关产品推荐
相关产品推荐

