当我使用左连接和分组时,总和不起作用

时间:2015-06-24 11:14:51

标签: mysql sql group-by sum left-join

我有两个表:进口和订单:我分别附上了这些。 enter image description here

enter image description here

我想要以下内容:  1.导入表中相同product_id的总和  2.作为状态明智的订单表中相同product_id的总和。

我的查询是:

SELECT `Import`.*, 
    SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
    SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
    SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
    SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
    ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id`

此查询的结果:

Array
(
    [0] => Array
        (
            [Import] => Array
                (
                    [id] => 1
                    [category_id] => 2
                    [product_id] => 2
                    [amount] => 50
                    [cost] => 8320
                    [comment] => transportation and others cost: 100  
                    [created] => 2015-06-23 19:21:10
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 1
                    [confirmed] => 2
                    [canceled] => 0
                )

        )

    [1] => Array
        (
            [Import] => Array
                (
                    [id] => 2
                    [category_id] => 2
                    [product_id] => 2
                    [amount] => 15
                    [cost] => 3000
                    [comment] => 
                    [created] => 2015-06-22 18:10:36
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 1
                    [confirmed] => 2
                    [canceled] => 0
                )

        )

    [2] => Array
        (
            [Import] => Array
                (
                    [id] => 3
                    [category_id] => 2
                    [product_id] => 1
                    [amount] => 15
                    [cost] => 2000
                    [comment] => 
                    [created] => 2015-06-23 19:20:15
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 0
                    [confirmed] => 0
                    [canceled] => 0
                )

        )

)

我的预期结果是:

Array
(
    [0] => Array
        (
            [Import] => Array
                (
                    [id] => 1
                    [category_id] => 2
                    [product_id] => 2
                    [amount] => 65
                    [cost] => 8320
                    [comment] => transportation and others cost: 100  
                    [created] => 2015-06-23 19:21:10
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 1
                    [confirmed] => 2
                    [canceled] => 0
                )

        )


    [2] => Array
        (
            [Import] => Array
                (
                    [id] => 3
                    [category_id] => 2
                    [product_id] => 1
                    [amount] => 15
                    [cost] => 2000
                    [comment] => 
                    [created] => 2015-06-23 19:20:15
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 0
                    [confirmed] => 0
                    [canceled] => 0
                )

        )

)

我该怎么做?我尝试过不同的方式,例如:

SELECT `Import`.*, SUM(`Import`.`amount`) as total_import,
    SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
    SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
    SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
    SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
    ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id`

但没有运气:'(

2 个答案:

答案 0 :(得分:1)

我猜您想要了解每种产品的信息。如果是这样,您可以通过几种方式实现此目的。这是一个union所有方法:

SELECT product_id, sum(amount) as total_import,
       sum( case when status = 'sold' THEN pieces else 0 end) as total_sell,
       sum( case when status = 'No contact' THEN pieces else 0 end) as no_contact,
       sum( case when status = 'confirmed' THEN pieces else 0 end) as confirmed,
       sum( case when status = 'canceled' THEN pieces else 0 end) as canceled
from ((select i.product_id, amount, NULL as status, NULL as pieces
       from `amrajegeachi`.`imports` i
      ) union all
      (select o.product_id, NULL, o.status, o.pieces
       from `orders` o
      )
     ) io
group by product_id;

答案 1 :(得分:1)

SELECT `Import`.*, SUM(`Import`.`amount`) as total_import,
    SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
    SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
    SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
    SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
    ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id