gpt4 book ai didi

node.js - 如何正确编写此查询

转载 作者:行者123 更新时间:2023-12-03 22:33:37 25 4
gpt4 key购买 nike

我正在尝试在 Sequelize 中执行此查询但出现错误
询问

LineItem.findAll(
{
attributes: [
"orderId",
[fn("sum", col("quantity")), "quantity"]
],
include: [{
model: Order,
as: "lineItems",
attributes: ["invoiceId"]
}],
group: ["orderId"],
raw: true
});
错误
column \"lineItems.invoice_id\" must appear in the GROUP BY clause or be used in an aggregate function

最佳答案

您有两个选择,但首先您必须回答这个问题:您的查询中是否需要列 invoiceId
如果您的答案是肯定的,请尝试:

LineItem.findAll({
attributes: ['orderId', [fn('sum', col('quantity')), 'quantity']],
include: [
{
model: Order,
as: 'lineItems',
attributes: ['invoiceId'],
},
],
group: ['orderId', 'invoiceId'],
raw: true,
});

如果您的答案是否定的,请尝试:
LineItem.findAll({
attributes: ['orderId', [fn('sum', col('quantity')), 'quantity']],
include: [
{
model: Order,
as: 'lineItems',
},
],
group: ['orderId'],
raw: true,
});

关于node.js - 如何正确编写此查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64141215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com