在这种情况下,我想根据 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/