简体   繁体   English

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. 我想要实现的是找到所有销售特定品牌的商店,但是在结果和商店一起,我也希望该特定商店出售的其他品牌。

For example: 例如:

Brand1 is sold by StoreA, StoreB, StoreC. Brand1由StoreA,StoreB和StoreC出售。 The result should look something like.. 结果应该看起来像..

StoreA - Brand1, Brand2 商店A-品牌1,品牌2

StoreB - Brand1, Brand3 StoreB-品牌1,品牌3

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. 我想到的一件事是,是否应该在收集的商店中循环并找到品牌,但我不知道这样做的效率如何,因为该图可以有很多节点。

Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

You can try this query : 您可以尝试以下查询:

MATCH (:Brand {id:1})-[:SOLD_BY]->(s:Store)
RETURN s, [ (b)-[:SOLD_BY]->(s) | b] AS brands

See this link to have more information : https://neo4j.com/blog/cypher-graphql-neo4j-3-1-preview/ 请参阅此链接以获取更多信息: https : //neo4j.com/blog/cypher-graphql-neo4j-3-1-preview/

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM