问题
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