Sequelize 多个左连接

时间:2021-03-02 16:47:30

标签: javascript mysql sql sequelize.js erp

我有 ProductsqtyProductsOrdersOrderProducts 表 我想要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`;"

}

1 个答案:

答案 0 :(得分:0)

尝试将您的查询更改为使用嵌套的 SELECT,我认为使用带有 group by 的左连接可能是问题的原因,因为您有 qtyqtyProducts

中的 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
相关问题