【发布时间】:2021-03-02 16:47:30
【问题描述】:
我有 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`;"
}
【问题讨论】:
-
“但我得到一个错误”你可以分享这个错误吗?
-
我收到此错误消息
"sqlMessage": "Column 'qty' in field list is ambiguous" -
什么错误?您是否可以编辑问题以包含错误,而不是将其放在评论中?
-
我编辑这篇文章
标签: javascript mysql sql sequelize.js erp