我有 Products
、qtyProducts
、Orders
和 OrderProducts
表
我想要SUM(qtyBuyedProducts)
和SUM(qtySelledProducts)
SQL 中的真值查询是(两个左连接):
SELECT products.code, SUM(qtyProducts.qty) as 'qty_selled', SUM(ProductOrders.qty) as 'qty_buyed'
FROM products
LEFT JOIN qtyProducts on products.id = qtyProducts.productId
LEFT JOIN ProductOrders on products.id = ProductOrders.productId
GROUP BY products.id
我在 Sequelize 中尝试过:
product.findAll(
{
group:'id',
include: [
{
model: qtyProduct,
attributes: [[sequelize.fn('SUM', sequelize.col('qty')),'qty_buyed']],
required: false
},
{
model: ProductOrder,
attributes: [[sequelize.fn('SUM', sequelize.col('qty')),'qty_selled']],
required: false
}
]
})
但我收到一个错误
请帮我
我从“邮递员”那里收到了这个错误
{
"name": "SequelizeDatabaseError",
"parent": {
"code": "ER_NON_UNIQ_ERROR",
"errno": 1052,
"sqlState": "23000",
"sqlMessage": "Column 'qty' in field list is ambiguous",
"sql": "SELECT `product`.`id`, `product`.`code`, `product`.`name`, `product`.`nameAr`, `product`.`description`, `product`.`unitPriceBuy`, `product`.`warningQty`, `product`.`createdAt`, `product`.`updatedAt`, `qtyProducts`.`id` AS `qtyProducts.id`, SUM(`qty`) AS `qtyProducts.qty_buyed`, `ProductOrders`.`invoiceBuyedId` AS `ProductOrders.invoiceBuyedId`, `ProductOrders`.`productId` AS `ProductOrders.productId`, SUM(`qty`) AS `ProductOrders.qty_selled` FROM `products` AS `product` LEFT OUTER JOIN `qtyProducts` AS `qtyProducts` ON `product`.`id` = `qtyProducts`.`productId` LEFT OUTER JOIN `ProductOrders` AS `ProductOrders` ON `product`.`id` = `ProductOrders`.`productId` GROUP BY `id`;"
},
"original": {
"code": "ER_NON_UNIQ_ERROR",
"errno": 1052,
"sqlState": "23000",
"sqlMessage": "Column 'qty' in field list is ambiguous",
"sql": "SELECT `product`.`id`, `product`.`code`, `product`.`name`, `product`.`nameAr`, `product`.`description`, `product`.`unitPriceBuy`, `product`.`warningQty`, `product`.`createdAt`, `product`.`updatedAt`, `qtyProducts`.`id` AS `qtyProducts.id`, SUM(`qty`) AS `qtyProducts.qty_buyed`, `ProductOrders`.`invoiceBuyedId` AS `ProductOrders.invoiceBuyedId`, `ProductOrders`.`productId` AS `ProductOrders.productId`, SUM(`qty`) AS `ProductOrders.qty_selled` FROM `products` AS `product` LEFT OUTER JOIN `qtyProducts` AS `qtyProducts` ON `product`.`id` = `qtyProducts`.`productId` LEFT OUTER JOIN `ProductOrders` AS `ProductOrders` ON `product`.`id` = `ProductOrders`.`productId` GROUP BY `id`;"
},
"sql": "SELECT `product`.`id`, `product`.`code`, `product`.`name`, `product`.`nameAr`, `product`.`description`, `product`.`unitPriceBuy`, `product`.`warningQty`, `product`.`createdAt`, `product`.`updatedAt`, `qtyProducts`.`id` AS `qtyProducts.id`, SUM(`qty`) AS `qtyProducts.qty_buyed`, `ProductOrders`.`invoiceBuyedId` AS `ProductOrders.invoiceBuyedId`, `ProductOrders`.`productId` AS `ProductOrders.productId`, SUM(`qty`) AS `ProductOrders.qty_selled` FROM `products` AS `product` LEFT OUTER JOIN `qtyProducts` AS `qtyProducts` ON `product`.`id` = `qtyProducts`.`productId` LEFT OUTER JOIN `ProductOrders` AS `ProductOrders` ON `product`.`id` = `ProductOrders`.`productId` GROUP BY `id`;"
}
答案 0 :(得分:0)
尝试将您的查询更改为使用嵌套的 SELECT,我认为使用带有 group by 的左连接可能是问题的原因,因为您有
qty
和 qtyProducts
ProductOrders
字段
SELECT
products.code,
COALESCE(
(
SELECT
SUM(qty)
FROM
qtyProducts
WHERE
productId = products.id
)
, 0) AS qty_selled,
COALESCE(
(
SELECT
SUM(qty)
FROM
ProductOrders
WHERE
productId = products.id
)
, 0) AS qty_buyed
FROM
products
GROUP BY
products.id