您好我想尝试使用'WHERE AND'来匹配neo4j关系
我的示例relationiship是:'用户访问国家'
我这样创造它......
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
Run Code Online (Sandbox Code Playgroud)
然后我查询(This Works):
MATCH (u:User)-[r:Visits]->(c:Country) where c.Name='France' or c.Name='Spain' return u
Run Code Online (Sandbox Code Playgroud)
结果:向我显示访问过西班牙或法国的所有用户,即使他们只访问过两个国家/地区之一.
但我想要做的是完全相同的查询,但使用'AND'代替'OR'.我可以让用户访问"法国"和"西班牙".
MATCH (u:User)-[r:Visits]->(c:Country) where c.Name='France' AND c.Name='Spain' return u
Run Code Online (Sandbox Code Playgroud)
结果:找到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
Run Code Online (Sandbox Code Playgroud)
它认为头号更好,因为它更精确,可能更有效.
| 归档时间: |
|
| 查看次数: |
7551 次 |
| 最近记录: |