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

如何在SurrealDB中聚合数组值生成唯一值数组?

在SurrealDB中聚合数组字段为唯一值数组的方法

要将所有记录里的program数组字段聚合为一个包含唯一值的单一数组,可以使用SurrealDB的数组聚合与处理函数组合实现,针对你提供的数据集,具体查询语句如下:

SELECT array::distinct(array::flatten(collect(program))) AS program FROM person;

语句拆解:

  • collect(program):将所有person表记录中的program数组收集起来,形成一个二维数组,示例中会得到 [['flights','apps'], ['entertainment','flights']]
  • array::flatten():把收集到的二维数组扁平化为一维数组,结果为 ['flights','apps','entertainment','flights']
  • array::distinct():对一维数组进行去重处理,得到仅包含唯一值的数组
  • AS program:将最终结果的字段名指定为program,匹配预期输出格式

运行该查询后,会输出符合要求的结果:

program: ['flights','apps','entertainment']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 08:37:05