neo4j - Neo4J 查询不需要关系
问题描述
我正在使用这个查询来获取一个家庭的家谱,如果每个分支都有 3 代,它可以正常工作,但只要一代没有孩子,就不会得到其他分支的孩子。这是我的查询
Match p=(a:Person)-[a1:PARENT_OF]->(b:Person)-[b1:PARENT_OF]->(c:Person)
where a.ID='9101'
WITH a,b,a1,b1,{id:c.ID,name:c.TEN,gioitinh:c.GIOI_TINH,loaiqh:b1.type} as thehe1
WITH a,a1,b1,{id:b.ID,name:b.TEN,gioitinh:b.GIOI_TINH,loaiqh:a1.type,loaiqh1:b1.type, children:collect(thehe1)} as thehe2
WITH {id:a.ID,name:a.TEN,gioitinh:a.GIOI_TINH,loaiqh:a1.type, children:collect(thehe2)} as phahe
return phahe
当一个或多个人 (b) 没有孩子时会发生这种情况,这意味着在这种情况下不存在关系 (b1) 和 (c)。Query 将为所有 Person (b) 返回 2 代,但并非所有 (b) 都没有使我的 Query 缺少数据的子代。
我应该怎么做才能使关系(b1)不需要?对不起我的英语不好,谢谢!
解决方案
这可能对您有用:
MATCH p=(a:Person)-[a1:PARENT_OF]->(b:Person)
WHERE a.ID='9101'
OPTIONAL MATCH (b)-[b1:PARENT_OF]->(c:Person)
WITH a, a1,
{id:b.ID, name:b.TEN, gioitinh:b.GIOI_TINH, loaiqh:a1.type,
loaiqh1: CASE WHEN b1 IS NOT NULL THEN b1.type END,
children: CASE WHEN b1 IS NOT NULL THEN COLLECT({id:c.ID,name:c.TEN,gioitinh:c.GIOI_TINH,loaiqh:b1.type}) ELSE [] END} as thehe2
RETURN {id:a.ID,name:a.TEN,gioitinh:a.GIOI_TINH,loaiqh:a1.type, children:collect(thehe2)} as phahe
推荐阅读
- c - 为什么 C 中的 sizeof 运算符在 NOT 运算的情况下会给出不同的输出?
- laravel - laravel 过滤关系
- ruby-on-rails - Rails 和 sidekiq:是否可以为工作分配名称?
- python-3.x - 在图书推荐系统中形成训练矩阵时出错
- react-native - NavigationsActions.Navigate 不工作
- algorithm - 计算矩形选择所选择的所有框的算法
- typo3 - 如何解决 VHS ViewHelper v:replace 的一个奇怪问题?
- python - 如何在 Python 的 notify_run 中获取当前注册的频道
- c# - 使用 AngleSharp 和 CSS 选择器的 C# Web 抓取知识面板/Google 页面
- reactjs - 单击 BrowserRouter 链接时隐藏全屏导航