MYSQL UNION - 产生意外结果

时间:2014-09-02 12:56:02

标签: mysql sql

我有以下MYSQL查询

MYSQL

SELECT SUM(shop_order_price), p.category
from shop_orders so
INNER JOIN product p
on so.shop_order_product_id=p.product_id
WHERE so.shop_order_action = 'Sale'
AND so.brand = '53'
AND p.category = 'Mens'
AND so.shop_order_location = 'Bawtry'
AND so.shop_order_month = '05'  AND so.shop_order_year = '2014'

UNION ALL

    SELECT SUM(shop_order_price), p.category
    from shop_orders so
    INNER JOIN product p
    on so.shop_order_product_id=p.product_id
    WHERE so.shop_order_action = 'Sale'
    AND so.brand = '53'
    AND p.category= 'Ladies'
    AND so.shop_order_location = 'Bawtry'
    AND so.shop_order_month = '05'  AND so.shop_order_year = '2014'

    UNION ALL

    SELECT SUM(shop_order_price), p.category
    from shop_orders so
    INNER JOIN product p
    on so.shop_order_product_id=p.product_id
    WHERE so.shop_order_action = 'Sale'
    AND so.brand = '53'
    AND so.shop_order_location = 'Bawtry'
    AND so.shop_order_month = '05'  AND so.shop_order_year = '2014'

产生以下结果:

RESULT

SUM(shop_order_price)        category
--------------------         ---------

282.5                         Mens
751                           Ladies
1033.5                        Ladies

SUM数字是正确的,但是,尽管查询中未指定类别,但底部行在类别列中显示“Ladies”。

我的问题是,我可以在最后一行显示TOTAL而不是Ladies吗?所以结果如下:

预期结果

 SUM(shop_order_price)        category
    --------------------         ---------

    282.5                         Mens
    751                           Ladies
    1033.5                        TOTAL

任何建议都会很棒。

2 个答案:

答案 0 :(得分:2)

这不是一个优雅的解决方案,但你可以试试这个。它不是为它分配一个列值,而是具有“总计”和“#39; TOTAL'硬编码,我认为应该导致你追求的目标。

<强> MYSQL

SELECT SUM(shop_order_price), p.category
from shop_orders so
INNER JOIN product p
on so.shop_order_product_id=p.product_id
WHERE so.shop_order_action = 'Sale'
AND so.brand = '53'
AND p.category = 'Mens'
AND so.shop_order_location = 'Bawtry'
AND so.shop_order_month = '05'  AND so.shop_order_year = '2014'

UNION ALL

    SELECT SUM(shop_order_price), p.category
    from shop_orders so
    INNER JOIN product p
    on so.shop_order_product_id=p.product_id
    WHERE so.shop_order_action = 'Sale'
    AND so.brand = '53'
    AND p.category= 'Ladies'
    AND so.shop_order_location = 'Bawtry'
    AND so.shop_order_month = '05'  AND so.shop_order_year = '2014'

    UNION ALL

    SELECT SUM(shop_order_price), 'Total'
    from shop_orders so
    INNER JOIN product p
    on so.shop_order_product_id=p.product_id
    WHERE so.shop_order_action = 'Sale'
    AND so.brand = '53'
    AND so.shop_order_location = 'Bawtry'
    AND so.shop_order_month = '05'  AND so.shop_order_year = '2014'

答案 1 :(得分:1)

您可以将GROUP BYWITH ROLLUP修饰符一起使用,以简化查询:

SELECT SUM(shop_order_price), p.category
FROM shop_orders so
  INNER JOIN product p
  ON so.shop_order_product_id = p.product_id
WHERE so.shop_order_action = 'Sale'
  AND so.brand = '53'
  AND p.category IN ('Mens', 'Ladies')
  AND so.shop_order_location = 'Bawtry'
  AND so.shop_order_month = '05'  
  AND so.shop_order_year = '2014'
GROUP BY p.category
  WITH ROLLUP ;

如果您不想显示NULL'TOTAL',则最好是在应用程序端。你可以在SQL中执行它,在派生表中包装查询 - 但我不会使用它:

SELECT
    sum_order_price, 
    COALESCE(category, 'TOTAL') AS category
FROM
  ( SELECT SUM(shop_order_price) AS sum_order_price,
           p.category
    FROM shop_orders so
      INNER JOIN product p
      ON so.shop_order_product_id = p.product_id
    WHERE so.shop_order_action = 'Sale'
      AND so.brand = '53'
      AND p.category IN ('Mens', 'Ladies')
      AND so.shop_order_location = 'Bawtry'
      AND so.shop_order_month = '05'  
      AND so.shop_order_year = '2014'
    GROUP BY p.category
      WITH ROLLUP
  ) AS dt ;