Neo4J:在Cypher中来回加入

时间:2023-01-19 18:02:12

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

我在Neo4J DB(Northwind DB)中有以下节点:

  • Customer
  • 顾客
  • Order
  • 订购
  • Product
  • 产品

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

节点具有以下关系:(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?

我如何获得客户购买的产品和产品数量,他们还购买了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)

2 个解决方案

#1


0  

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

您需要在使用WITH传递产品ID之前过滤产品ID:

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

#2


0  

The reason why my query did not work, was that I used count(pr.productName) instead of count(DISTINCT od).

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

#1


0  

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

您需要在使用WITH传递产品ID之前过滤产品ID:

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

#2


0  

The reason why my query did not work, was that I used count(pr.productName) instead of count(DISTINCT od).

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