简体   繁体   中英

Neo4J: Joining back and forth in Cypher

I have the following Nodes in a Neo4J DB (Northwind DB):

  • Customer
  • Order
  • Product

The nodes have the following relationship:
(c:Customer)-->(o:Order)--> (p:Product)

How can I get the Products , and the count of the products, that have been bought by Customers , who also bought a Product with pr_ID =2?

I have tried the following query. It returns the correct items, but the wrong counts:

MATCH (p:Product)<--(o:Order)<--(c:Customer) 
WITH p,o,c WHERE p.productID='2' 
MATCH c-->(od:Order)-->(pr:Product) 
WITH c,od,pr WHERE NOT pr.productID='2' 
RETURN pr.productName, count(pr.productName)

you need to filter the product id before passing them with the WITH :

MATCH (c:Customer)-->(o:Order)-->(p:Product {productId: '2'})
MATCH (c)-->(:Order)-->(p2:Product)
WHERE p2 <> p
RETURN p2.productName, count(*) as c

我的查询不起作用的原因是,我使用了count(pr.productName)而不是count(DISTINCT od)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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