Postgres查询使用Knex删除多个连接数组中的重复项

达斯汀·斯诺普(Dustin Snoap)

我正在使用knex来构建一个postgres查询,并拥有一个配方表,该表与配料表和步骤都具有多对多的关系(每个步骤都是指令的一部分)。我正在尝试将步骤和成分都汇总到查询中自己的数组中。我的问题是,一旦我加入了第二个数组,两个数组就失去了独特性(即表a有2个元素,表b有3个元素;加入表b之后,两个数组现在都有6个元素)。

我试过使用distinct,但是每次尝试都导致抛出错误。

这是我要输出的内容:

 "id": 1,
  "title": "sometitle",
  "ingredients": [
    {
      "ingredient": "avacado",
      "quantity": 24
    },
    {
      "ingredient": "asparagus",
      "quantity": 42
    },
  ],
  "instructions": [
    {
      "step": 1,
      "instruction": "one"
    },
    {
      "step": 2,
      "instruction": "two"
    },
    {
      "step": 3,
      "instruction": "three"
    },
  ]

这是我到目前为止的内容:

knex(`recipes as r`)
    .where({'r.id': 1})
    .join('ingredients_list as list', {'list.recipe_id': 'r.id'})
    .join('ingredients', {'list.ingredient_id': 'ingredients.id'})
    .join('instructions', {'instructions.recipe_id': 'r.id'})
    .select(
        'r.id',
        db.raw(`json_agg(json_build_object(
            'ingredient', ingredients.name,
            'quantity', list.quantity
            )) as ingredients`),
        db.raw(`json_agg(json_build_object(
            'step', instructions.step_number,
            'instruction', instructions.description
            )) as instructions`)
    )
    .groupBy('r.id')
    .first()
达斯汀·斯诺普(Dustin Snoap)

这是我想出的解决方案,以防其他人遇到此问题。我认为这是可行的,因为postgres无法评估json对象的相等性。而jsonb是一个二进制对象。我希望对此有一个更彻底的解释是有人拥有。

distinct json_agg(jsonb_build_object(...))
knex(`recipes as r`)
    .where({'r.id': 1})
    .join('ingredients_list as list', {'list.recipe_id': 'r.id'})
    .join('ingredients', {'list.ingredient_id': 'ingredients.id'})
    .join('instructions', {'instructions.recipe_id': 'r.id'})
    .select(
        'r.id',
        db.raw(`distinct json_agg(jsonb_build_object(
            'ingredient', ingredients.name,
            'quantity', list.quantity
            )) as ingredients`),
        db.raw(`distinct json_agg(jsonb_build_object(
            'step', instructions.step_number,
            'instruction', instructions.description
            )) as instructions`)
    )
    .groupBy('r.id')
    .first()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

连接Postgres查询中的结果集,并删除重复项

来自分类Dev

postgres:交叉表并删除查询中的重复项

来自分类Dev

删除postgres中的重复项

来自分类Dev

删除postgres中的重复项

来自分类Dev

从数组中删除重复项

来自分类Dev

重复时从查询中删除重复项

来自分类Dev

使用聚合查询删除mongodb中的重复项

来自分类Dev

从字符串数组中删除重复项-使用HashSet

来自分类Dev

使用lodash.js删除对象数组中的重复项

来自分类Dev

在Visualforce组件上使用从数组中删除重复项

来自分类Dev

如何使用JavaScript从数组中删除重复项

来自分类Dev

Java使用循环从数组中删除重复项

来自分类Dev

在Visualforce组件上使用从数组中删除重复项

来自分类Dev

使用lodash.js删除对象数组中的重复项

来自分类Dev

使用回调从数组中删除重复项

来自分类Dev

使用 Foreach 循环从多维数组中删除重复项

来自分类Dev

使用纯 Javascript 或 Typescript 删除数组中的重复项

来自分类Dev

使用 forEach 删除数组中的重复项

来自分类Dev

使用 Set 从数组中删除重复项,保持最高 id

来自分类Dev

连接数组中的三个查询

来自分类Dev

从数组数组中删除重复项

来自分类Dev

如何从UNION查询中“删除重复项”

来自分类Dev

从查询结果中删除重复项

来自分类Dev

SQL从查询结果中删除重复项

来自分类Dev

如何从UNION查询中“删除重复项”

来自分类Dev

从单列电源查询中删除重复项

来自分类Dev

删除 CONNECT BY LEVEL 查询中的重复项

来自分类Dev

从选择查询中删除重复项

来自分类Dev

从Java中的数组中删除重复项