我在Neo4J中运行以下查询,但是收到错误。简而言之,我要求数据库向我展示标签为“user”的所有节点,如果它有关系,则表示关系的类型和它所连接的节点的标签类型。
START n=node(*) MATCH (n:user)-[r?]-(m) WHERE HAS (n.name) RETURN n, labels(n), type(r), labels(m), m.name;
当找到类型为user的节点没有与之相关的节点时,会发生错误。它似乎在标签(m)部分爆炸。我希望如果节点不存在,我会返回NULL而不是错误。这就是type()函数的工作方式。如果没有找到关系,则type()返回NULL。
如果省略标签(m)部分......
,我的结果如下==>+-----------------------------------------------------------------------------------------------------------------------------------+
==> | n | labels(n) | type(r) | m.name |
==> +-----------------------------------------------------------------------------------------------------------------------------------+
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | "vinniecontext" |
==> | Node[2558]{lastName:"b",mobilePhone:"b",status:"b",email:"b",name:"b",firstName:"b"} | ["user"] | <null> | <null> |
==> | Node[2559]{lastName:"c",mobilePhone:"c",status:"c",email:"c",name:"c",firstName:"c"} | ["user"] | <null> | <null> |
==> | Node[2560]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | <null> | <null> |
==> | Node[2561]{lastName:"b",mobilePhone:"b",status:"b",email:"b",name:"",firstName:"b"} | ["user"] | <null> | <null> |
==> +-----------------------------------------------------------------------------------------------------------------------------------+
如何获得我想要的结果?在没有连接节点的情况下我基本上想要一个NULL值,否则给我连接节点的标签。
提前致谢!!
答案 0 :(得分:2)
好的,所以同事帮我解决了这个问题(在我把头撞到桌子上一天半之后)。我通过使用UNION ALL获得了我想要的结果。我必须做两个不同的查询;一个关系为null,另一个关系不为null。
START n=node(*) MATCH (n:user)-[r?]-(m) WHERE r IS NOT NULL RETURN n, labels(n), type(r), labels(m) as i, m.name as a
UNION ALL
START n=node(*) MATCH (n:user)-[r?]-(m) WHERE r IS NULL RETURN n, labels(n), type(r), null as i, null as a;
这是结果......
==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
==> | n | labels(n) | type(r) | i | a |
==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | ["context"] | "vinniecontext" |
==> | Node[2558]{lastName:"b",mobilePhone:"b",status:"b",email:"b",name:"b",firstName:"b"} | ["user"] | <null> | <null> | <null> |
==> | Node[2559]{lastName:"c",mobilePhone:"c",status:"c",email:"c",name:"c",firstName:"c"} | ["user"] | <null> | <null> | <null> |
==> | Node[2560]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | <null> | <null> | <null> |
==> | Node[2561]{lastName:"b",mobilePhone:"b",status:"b",email:"b",name:"",firstName:"b"} | ["user"] | <null> | <null> | <null> |
==> | Node[7638]{lastName:"afdasdfasdf",mobilePhone:"asdfa",status:"asdfasdf",email:"asdfasdfasdf",name:"asdfasdfa",firstName:"asdfasdfas"} | ["user"] | <null> | <null> | <null> |
==> | Node[7639]{lastName:"asddsafsda",mobilePhone:"asdfasdfdsa",status:"sadfsadfsda",email:"sadfasdfas",name:"sdafasdfsadf",firstName:"sadfsadfsad"} | ["user"] | <null> | <null> | <null> |
==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
我希望这个解决方案可以为别人带来一些痛苦。