gpt4 book ai didi

Neo4j,匹配关系 WHERE AND

转载 作者:行者123 更新时间:2023-12-01 07:18:40 25 4
gpt4 key购买 nike

您好,我正在尝试使用“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

然后我查询(This Works):
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

它认为第一更好,因为它更精确,可能更有效。

关于Neo4j,匹配关系 WHERE AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27433808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com