【问题标题】:Sequelize Subqueries序列化子查询
【发布时间】:2021-02-20 23:58:45
【问题描述】:

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

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

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    您需要在外部查询中朗姆酒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,
      },
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 2016-12-22
      • 2011-05-22
      • 1970-01-01
      • 2013-11-02
      相关资源
      最近更新 更多