问题描述
当我在寻找 2 个关系时,我想出了如何编写此查询,但不确定如何向查询添加更多关系.
I figured out how to write this query when I am looking for 2 relationships, but not sure how to add more relationships to the query.
假设您有一个以reader"和book"为节点的读书俱乐部数据库.'book' 节点有一个 'genre' 属性(用于定义这本书是小说、非小说、传记、参考文献等)在读者"节点和书"节点之间存在关系HasRead",其中有人读过一本书.
Assume you have a book club database with 'reader' and 'book' as nodes. The 'book' nodes have a 'genre' attribute (to define that the book is a Fiction, Non-Fiction, Biography, Reference, etc.) There is a Relationship "HasRead" between 'reader' nodes and 'book' nodes where someone has read a particular book.
如果我想找到同时阅读小说和非小说书籍的读者,我可以执行这个 Cypher 查询:
If I want to find readers that have read both Fiction AND Non-Fiction books, I could execute this Cypher query:
Start b1=node:MyBookIndex('Genre:Fiction'),
b2=node:MyBookIndex('Genre:Non-Fiction')
Match b1-[:HadRead]-r-[:HasRead]-b2
Return r.ReaderName
上述查询的关键是 Match 子句,该子句将两个书籍别名输入到读者"节点的 r
别名中.
The key to the above query is the Match clause that has the two book aliases feeding into the r
alias for the 'reader' nodes.
问题:我将如何编写查询以查找已阅读小说AND 非小说AND 参考书的用户?当您要查找的内容超过 2 件时,我一直在思考如何编写 Match 子句.
Question: How would I write the query to find users that have read Fiction AND Non-Fiction AND Reference books? I'm stuck with how you would write the Match clause when you have more than 2 things you are looking for.
推荐答案
您可以在单个 MATCH
子句中指定多行,用逗号分隔.例如,以下两个 MATCH
子句在语义上是等效的(并且将由引擎进行相同的评估):
You can have multiple line specified in a single MATCH
clause, separated by commas. For example, the following two MATCH
clauses are semantically equivalent (and will be evaluated identically by the engine):
//these mean the same thing!
match a--b--c
match a--b, b--c
您可以拥有任意数量的这些匹配项.因此,将其插入您的查询中,您会得到:
You can have any number of these matches. So, plugging that into your query, you get this:
start b1=node:MyBookIndex('Genre:Fiction'),
b2=node:MyBookIndex('Genre:Non-Fiction'),
b3=node:MyBookIndex('Genre:Reference')
match b1-[:HasRead]-r,
b2-[:HasRead]-r,
b3-[:HasRead]-r
return r.ReaderName
这篇关于Cypher 查询以查找具有 3 个关系的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!