您好,我正在尝试使用“ WHERE AND”匹配neo4j关系
我的示例关系是:“用户访问国家/地区”
我是这样创建的...
MATCH (c:Country{Name:Country}) MERGE (u:User{Email:Email,UserID: UserID}) MERGE (u)-[r:Visits]->(c)
//Countries are previously created and Users may or may not exist
然后我查询(有效):
MATCH (u:User)-[r:Visits]->(c:Country) where c.Name='France' or c.Name='Spain' return u
结果:向我显示所有访问过西班牙或法国的用户,即使他们只访问了两个国家之一。
但是我想做的是完全相同的查询,但是用“ AND”而不是“ OR”。在其中,我可以吸引访问过“法国”和“西班牙”的用户。
MATCH (u:User)-[r:Visits]->(c:Country) where c.Name='France' AND c.Name='Spain' return u
结果:找到0个节点和关系。
我能做些什么?
在您的查询中,您要匹配一个国家(地区)节点,并说该节点的名称必须为France
并且必须为Spain
。
您想要找到的是对法国和西班牙都有访问权的所有用户。有几种方法可以走...
//match both countries against the same user and identify them separtely
//making two matches in a single query
MATCH (u:User)-[:VISITS]->(c1:Country), (u)-[:VISITS]->(c2:Country)
WHERE c1.name = "France"
AND c2.name = "Spain"
RETURN u.name
//match all users that have been to either and only return the one that have been to both
MATCH (u:User)-[r:VISITS]->(c:Country)
WHERE (c.name IN [ "France", "Spain"])
WITH u, count(*) AS num
WHERE num = 2
RETURN u.name, num
它认为第一更好,因为它更精确,而且可能更有效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句