Neo4j Cypher query for many to many relationship

℡╲_俬逩灬. 提交于 2019-12-24 07:03:08

问题


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. The result should look something like..

StoreA - Brand1, Brand2

StoreB - Brand1, Brand3

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.


回答1:


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/

Cheers



来源:https://stackoverflow.com/questions/45392402/neo4j-cypher-query-for-many-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!