MATCH条款中的连词?

时间:2013-10-05 14:58:08

标签: neo4j cypher

你如何or条件而不是and?示例如下:

START user = node({id})
MATCH
(user)-[:follows]->(followed),
(follower)-[:follows]->(user)
RETURN user, followed, follower

我想要回复的是:user,无论是否有人关注她或是否跟随任何人。全部followers,如果有的话。全部followed,如果有的话。

上面的查询就像是and一样。如果user不跟随任何人,或者没有人跟随用户,则不返回任何内容。


这是我尝试过的其他内容,但我收到了语法错误:

start a = node(40663) 
with a, a as b 
match (b)-[:follows]->(c) 
with b, a as d 
(e)-[:follows]->(d) 
return a, c, e;

错误:

SyntaxException: string matching regex `$' expected but `(' found

Think we should have better error message here? Help us by sending this query to cypher@neo4j.org.

Thank you, the Neo4j Team.

"start a = node(40663) with a, a as b match (b)-[:follows]->(c) with b, a as d (e)-[:follows]->(d) return a, c, e"
                                                                               ^

无论此错误如何,减少的查询(只有一个with)都会返回零结果,因此这似乎也不是这样做的。

3 个答案:

答案 0 :(得分:1)

你能做到:

START user = node({id})
MATCH
p1 = (user)-[:follows]->(followed),
p2 = (follower)-[:follows]->(user)
RETURN user, followed, follower

或者:

START user = node({id})
MATCH (user)-[:follows]->(f)
WITH user, collect(f) as followed 
MATCH (f)-[:follows]->(user)
RETURN user, followed, collect(f) as follower

答案 1 :(得分:0)

好的,我可以用这样的东西解决这个问题:

start a = node(40663)
match (a)-[?:follows]->(b), (c)-[?:follows]->(a) 
return a, b, c;

但如果有更好的方法,请不要犹豫!

答案 2 :(得分:0)

匹配(c)< - [?: follow] - (a) - [?: follow] - >(b) 也应该工作

编辑: 可替代地

START user = node({id})
MATCH user--m 
WHERE user-[:follows]->m 
OR user<-[:followed]-m 
RETURN user  ,m