简体   繁体   English

neo4j cypher返回顶部节点或起始节点

[英]neo4j cypher return top node or start node

I have a hierarchical org structure like this: 我有一个像这样的分层组织结构:

OrgNode(3)-[HAS_PARENT]->OrgNode(2)-[HAS_PARENT]->OrgNode(1)

I want a cypher query that gives me the top org given any of the node ids: 我想要一个密码查询,该查询可为我提供给出任何节点ID的顶级组织:

topOrg(3) = OrgNode(1)
topOrg(2) = OrgNode(1)
topOrg(1) = OrgNode(1)

I'm able to write the query to return the top org when the starting node has at least one parent. 当起始节点至少有一个父级时,我可以编写查询以返回顶级组织。 But I cannot figure out how to return starting node when there is no parent link in the same query: 但是我无法弄清楚在同一查询中没有父链接时如何返回起始节点:

start n=node(3)
match (n)-[:PARENT*]->(m)-[r?:PARENT]->()
WHERE r is null
return m

You might use the UNION operator to combine your result with the result of another query that handles a starting node without a parent, 您可以使用UNION运算符将您的结果与另一个查询的结果合并,该查询处理没有父节点的起始节点,

start n=node(3)
match (n)-[:PARENT*]->(m)-[r?:PARENT]->()
WHERE r is null
return m as result
UNION 
Start n=node(3)
Match n 
Where not(n-[:PARENT]-())
Return n as result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM