【问题标题】:Neo4j - Many count nodes by property value in the same cypher queryNeo4j - 许多在同一个密码查询中按属性值计算节点
【发布时间】:2018-12-12 20:59:40
【问题描述】:

让我们有一个节点Reference,其属性为rating。假设我们在数据库中有 2 个引用,2 个“阳性”和 1 个“阴性”。

Cypher 查询计数正面引用:

MATCH (positive:Reference{rating:'Positive'}) return count(positive)

返回 2,确定

Cypher 查询以计算 否定 引用:

MATCH (negative:Reference{rating:'Negative'}) return count(negative)

返回 1,确定

Cypher 查询同时计算正面和负面引用。基本上,将前面的两个查询合并到一个查询中。

MATCH (positive:Reference{rating:'Positive'})
MATCH (negative:Reference{rating:'Negative'})
return count(positive) as positive, count(negative) as negative

我预计返回的值为正 2 和负 1。但是,两个结果都是 2。

我错过了什么吗?可能我错了,但我发誓这适用于旧版本的 neo4j(现在我使用的是 3.3.2)。

提前感谢您的帮助。

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    请记住,当您进行这样的背靠背匹配时,当您正在查找的事物之间没有关系时,您将得到 positive 的所有元素与 negative 的所有元素的叉积。因此,在这种情况下,由于有 2 个肯定响应和 1 个否定响应,因此您总共有 2 x 1 = 2 行(单个否定节点将列在每一行上)。您可以将退货更改为RETURN positive, negative,然后在表格结果视图中查看。

    要获得您想要的结果,您可以获取不同节点的数量:

    RETURN count(DISTINCT positive) as positive, count(DISTINCT negative) as negative
    

    或者使用更好的方法,在比赛结束后立即收集,这最终会减少工作量:

    MATCH (positive:Reference{rating:'Positive'})
    WITH count(positive) as positive
    MATCH (negative:Reference{rating:'Negative'})
    RETURN positive, count(negative) as negative
    

    【讨论】:

    • 非常感谢您快速而详细的回复!它就像一个魅力!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    相关资源
    最近更新 更多