【问题标题】:How to know if a Node x has every instance of node z where it has many relationship to another node y that has one relationship to another node z?如何知道节点 x 是否具有节点 z 的每个实例,其中它与另一个节点 y 有很多关系,而另一个节点 y 与另一个节点 z 有一个关系?
【发布时间】:2018-05-10 05:29:48
【问题描述】:

所以我有一个节点:Customer,它有很多节点:Order,而:Order 必须与节点:Shipper 有1 个关系,称为:SHIP_VIA,这是层次结构:

(customer:Customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->(shipper:Shipper)

现在只有 3 个托运人,订单指向他们,一个客户会有很多订单。所以我的问题是如何让所有客户都与所有托运人一起发货?请注意,每个托运人都有一个shipperID 属性。这是我的代码,但它不工作,除非有一个“存在”

match (customer:Customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->(shipper:Shipper) 

WHERE exists((customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->(:Shipper 

{shipperID:1})) and exists((customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->

(:Shipper {shipperID:2})) and exists((customer)-[:PURCHASED]->(order)-

[:SHIP_VIA]->(:Shipper {shipperID:3})) return  customer, order , shipper;

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    使用collect()all() 函数尝试此解决方案:

    // match all shippers
    match (shipper:Shipper)
    // create a list containing all shippers and pass to the next context
    with collect(shipper) as shippers
    
    // match all customers which has purchased an order with each shipper
    match (customer:Customer)
    where all (shipper in shippers where (customer)-[:PURCHASED]->(:Order)-[:SHIP_VIA]->(shipper))
    
    // return customers
    return customer
    

    【讨论】:

    • 谢谢你完全奏效,也感谢cmets的理解。
    • 嗨@OmarBassyouni,不客气!此外,如果此答案解决了您的问题,请通过单击复选标记考虑 accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。谢谢!
    • @OmarBassyouni 如果它解决了您的问题,请确保接受答案(绿色勾选它)。仅供参考,接受答案会导致: - 给予回答者 +15 声望点。 - 给你(询问者)+2 声望点。 - 最重要的是,它向观众表明此答案是解决此问题的适当解决方案。
    【解决方案2】:
    1. 计算托运人总数
    2. 对于每个客户,验证唯一托运人的数量是否等于托运人的总数

    MATCH (shipper:Shipper) 
    WITH count(shipper) AS totalShippers
    
    MATCH (customer:Customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->(shipper:Shipper)
    WITH totalShippers, customer, 
         COUNT(DISTINCT shipper) as shippers WHERE totalShippers = shippers
    
    RETURN customer
    

    【讨论】:

    • 此代码有效,谢谢。但是如果你添加一些 cmets 来理解它会非常棒。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多