0

我在 Neo4J 中运行以下查询,但出现错误。简而言之,我要求数据库向我显示所有带有标签“用户”的节点,如果它有关系,则显示关系的类型和它所连接的节点的标签类型。

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值,否则给我连接节点的标签。

提前致谢!!

4

1 回答 1

2

好的,所以一位同事帮我解决了这个问题(在我把头撞在桌子上一天半之后)。通过使用 UNION ALL,我得到了我想要的结果。我必须做两个不同的查询;一种关系为空,一种关系不为空。

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>          |
==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 

我希望这个解决方案可以减轻其他人的痛苦。

于 2013-07-24T15:04:15.457 回答