node.js - 序列化子查询

标签 node.js sequelize.js

在这种情况下,我想根据 orderId 找到每个订单的总准备时间,但是当我这样写时,它只显示第一个结果,

 let prepareOrder = await OrderItem.findAll({
          
    where: {
            options: null,
          },
    
          attributes: ["orderId"],
    
          include: [
            {
              model: Item,
              attributes: [
                [
                  sequelize.fn("sum", sequelize.col("prepareTime")),
                  "totalPrepareTime",
                ],
    
              ],
    
            },
          ],
        });

最佳答案

您需要在外部查询上 朗姆酒 SUM() 。当您在内部查询上运行它时,它返回单行,然后执行 JOIN 这就是为什么您只得到一行的原因。

const prepareOrder = await OrderItem.findAll({
  attributes: [
    "orderId",
    // get the summary at the OrderItem for each related Item
    [
      sequelize.fn("sum", sequelize.col("item.prepareTime")),
      "totalPrepareTime",
    ],
  ],
  // include Item but no attributes for performance
  include: {
    model: Item,
    attributes: [],
  },
  where: {
    options: null,
  },
});

关于node.js - 序列化子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64745317/

相关文章:

node.js - nodejs + sequelize::include where required false

javascript - 类型错误 : Object is not a function Node JS

javascript - 在插件中使用 nan 接收和返回 Float32Array

node.js - Rollup 无法看到 commonjs 模块的命名导出

mysql - 如何在 node.js 的 typescript-sequelize 中使用 'or' 选项?

javascript - Sequelize js中的Datediff

javascript - 函数不会等到其他函数执行完毕

node.js - 尝试运行 socket.io 示例时出现跨源请求被阻止错误

node.js - 可以使用 Sequelize 连接到新数据库吗?

node.js - 错误 : XModel is not associated to Ymodel on Sequelizejs