Neo4j Cypher查询多对多关系 [英] Neo4j Cypher query for many to many relationship
问题描述
我有一个如下图的图形:
I have a graph that looks like the following:
Brand--SOLD_BY-->Store
某个商品的一个品牌可以由多家商店出售.同样,一家商店可以出售多个品牌的商品.
One brand of a certain item can be sold by multiple stores. Similarly, a single store can sell multiple brands of items.
我要实现的目标是找到所有销售特定品牌的商店,但在结果以及商店中,我也希望该特定商店出售的其他品牌.
What I want to achieve is find all the stores that sell a particular brand but in the result along with the store, I would also like the other brands that are sold by that particular store.
例如:
品牌1由StoreA,StoreB和StoreC出售.结果应该看起来像..
Brand1 is sold by StoreA, StoreB, StoreC. The result should look something like..
StoreA-品牌1,品牌2
StoreA - Brand1, Brand2
StoreB-Brand1,Brand3
StoreB - Brand1, Brand3
StoreC-Brand1
StoreC - Brand1
我设法找到了商店,但找不到商店出售的其他品牌.
I've managed to find the stores but I'm not being able to find the other brands sold by the store.
MATCH (b:Brand)-[s:SOLD_BY]->(s:Store)
WHERE b.id=1
WITH DISTINCT s AS stores
RETURN stores
我想到的一件事是,是否应该在收集的商店中循环并找到品牌,但我不知道这样做的效率如何,因为图表可以有很多节点.
One thing I've thought of is if I should loop inside the collected stores and find the brands but I don't know how efficient that would be since the graph can have many nodes.
感谢您的任何帮助.
推荐答案
您可以尝试以下查询:
MATCH (:Brand {id:1})-[:SOLD_BY]->(s:Store)
RETURN s, [ (b)-[:SOLD_BY]->(s) | b] AS brands
请参阅此链接以获取更多信息: https://neo4j.com/blog/cypher-graphql-neo4j-3-1-preview/
See this link to have more information : https://neo4j.com/blog/cypher-graphql-neo4j-3-1-preview/
欢呼
这篇关于Neo4j Cypher查询多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!