You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypher含DISTINCT的RETURN语句无法排序问题求助

Cypher查询中RETURN DISTINCT后ORDER BY报错的解决方法

问题场景

执行以下Cypher查询时触发错误:

WITH $arrayHere as ids
UNWIND ids as id
MATCH(o:`App`)-[:ContainsFood]->(d:Food:`App`) WHERE id(o) = id
RETURN DISTINCT {Document: d} ORDER BY d.Title

错误信息

In a WITH/RETURN with DISTINCT or an aggregation, it is not possible to access variables declared before the WITH/RETURN: d (line 5, column 42 (offset: 308))
" RETURN DISTINCT {Food: d} ORDER BY d.Title"

解决办法

调整查询逻辑,先通过WITH DISTINCT对d进行去重并保留变量,再执行返回和排序:

WITH $arrayHere as ids
UNWIND ids as id
MATCH(o:`App`)-[:ContainsFood]->(d:Food:`App`) WHERE id(o) = id
WITH DISTINCT d
RETURN {Document: d} ORDER BY d.Title

原因说明

原查询中,RETURN DISTINCT {Document: d}会生成新的映射对象,此时原始变量d已不在当前作用域内,导致ORDER BY d.Title无法访问该变量。通过WITH DISTINCT d提前完成去重并保留d变量,后续的RETURN和ORDER BY就能正常访问d的属性,既实现去重又完成排序需求。

内容的提问来源于stack exchange,提问作者rohit bumrah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 08:35:24