分层 SOQL 查询
有谁知道如何根据帐户的 id 检索帐户的分层模型?
我尝试使用此查询,但我得到的只是第一组子节点。
select a.Name,a.parentId,a.ownerid,a.id from Account a where Parent.id ='00711314'
Does anyone know how to retrieve the hierarchical model of a account based on it's id?
I tried using this query but all i got is the first set of child nodes.
select a.Name,a.parentId,a.ownerid,a.id from Account a where Parent.id ='00711314'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SOQL 不支持分层检索,您必须逐级检索,为每个级别生成一个 id 列表,然后使用
in
where 运算符检索下一个级别。请记住,尽管存在调节器限制,但如果您有大树,您将很容易遇到限制。
SOQL does not support hierarchical retrieval, you have to retrieve level by level, for each level generate a list of id's and then retrieve next level using
in <list>
where operator.Keep in mind though that governor limitations apply and if you have large trees you'll run into a limit quite easily.
如前所述,您不能使用 SOQL 进行分层检索。当我需要与其他对象一起使用此功能时(并且当我知道有 <10k 行时),我选择了所有记录,然后使用列表映射来构建内存中的层次结构:
然后您可以使用例如,对数据执行操作等的递归函数(未经测试):
此代码效率低下且未经测试(我刚刚将此版本直接写入浏览器),但它应该让您了解如何处理平台上的分层数据。
As already stated you can not make use of hierarchical retrieval with SOQL. When I've needed this functionality with other objects (and when I know there are < 10k rows) I've selected all records, then used a map of lists to build up the hierarchy in memory instead:
You can then make use of a recursive function to perform operations etc. on the data, for instance (untested):
This code is inefficient and untested (I've just written this version straight into the browser) but it should give you an idea of how you can deal with hierarchical data on the platform.
如果您知道层次结构中的级别数量有限,并且您在每个级别查询有限数量的字段,则可以执行以下操作:
它很丑陋,但允许您在一个层次结构中获取固定数量的层次结构级别。单个查询。
If you know there is a limited number of levels in your hierarchy, and you are querying a limited number of fields at each level, you can do something like this:
It's ugly, but allows you to get a fixed number of hierarchy levels in a single query.