我用UNION ALL刮擦我的脑袋

时间:2017-03-10 10:41:21

标签: mysql sql

我根本不是SQL查询向导,这是我的问题:

我有3个单独的查询,效果非常好,每个查询都在我的网站上给我一个漂亮的框架和结果。

SELECT   arretsautressb AS Raison, SUM(minutesarrets) AS Minutes 
FROM     rapport_production_salles_blanches_2_repeat
GROUP BY Raison 
ORDER BY Minutes DESC

SELECT   redresseuseminutesarrets AS Raison, SUM(minutesarretsredresseuse) AS Minutes 
FROM     rapport_production_salles_blanches_3_repeat
GROUP BY Raison 
ORDER BY Minutes DESC

SELECT   raisonarretsconvoyeurair AS Raison, SUM(minutesarretsconvoyeurair) AS Minutes 
FROM     rapport_production_salles_blanches_4_repeat
GROUP BY Raison 
ORDER BY Minutes DESC

所以这3个结果一切都很好......我表中的Raison列返回所有行和Minutes查询SUM所有行Group by Raison ... 但我想合并这些查询,所以它只给我一张大表,而不是3张表。

但无论我如何尝试格式化我的UNION ALL代码,我得到的只是每个Raison查询的结果(因此在sql表中只需要1行),而不是分开时的所有行。但Minutes查询正在计算所有行的SUM。

如果有人会告诉我该怎么做的话会很酷...因为我已经阅读了几个小时的文档,而且我仍然坚持这个。

这是我到目前为止所尝试的,没有错误,但只从Sql表中获取了一行Raison,而不是所有行:

SELECT * 
FROM ( (SELECT arretsautressb AS Raison, 
               SUM(minutesarrets) AS Minutes 
        FROM   rapport_production_salles_blanches_2_repeat t1)
      UNION ALL
       (SELECT redresseuseminutesarrets AS Raison,
               SUM(minutesarretsredresseuse) AS Minutes 
        FROM   rapport_production_salles_blanches_3_repeat t2)
      UNION ALL
       (SELECT  raisonarretsconvoyeurair AS Raison,
                SUM(minutesarretsconvoyeurair) AS Minutes 
        FROM    rapport_production_salles_blanches_4_repeat t3)
     ) AS t123
GROUP BY Raison 
ORDER BY Minutes DESC

这是我从UNION ALL查询得到的:

UNION ALL

但这是我从3个分开的查询得到的:

3 querys

2 个答案:

答案 0 :(得分:2)

我认为由于以下原因,您的查询无法返回您想要的结果:

  • 使用子查询可以指定三个表并将它们联合起来。但是,如果不使用 GROUP BY ,则不能使用聚合(在本例中为SUM)。
  • 接下来,无论何时使用GROUP BY,都应该引用属性而不是列名。在我的查询中,我将 GROUP BY Raison 更改为 GROUP BY t1.arretsautressb
  • 我在外部查询上使用了ORDER BY,并按第二列排序,在本例中为SUM(minutesarrets)。

我将使用的查询如下:

SELECT  *
FROM    (
            SELECT  arretsautressb AS Raison
            ,       SUM(minutesarrets) AS sum_minutes
            FROM    rapport_production_salles_blanches_2_repeat AS t1
            GROUP BY t1.arretsautressb
            UNION ALL
            SELECT  redresseuseminutesarrets AS Raison
            ,       SUM(minutesarretsredresseuse) AS sum_minutes
            FROM    rapport_production_salles_blanches_3_repeat AS t2
            GROUP BY t2.redresseuseminutesarrets
            UNION ALL
            SELECT  raisonarretsconvoyeurair AS Raison
            ,       SUM(minutesarretsconvoyeurair) AS sum_minutes
            FROM    rapport_production_salles_blanches_4_repeat AS t3
            GROUP BY t3.raisonarretsconvoyeurair
        ) AS t123
ORDER BY 2 DESC

答案 1 :(得分:0)

试试这个:

SELECT * FROM (
    SELECT * FROM (
        (SELECT arretsautressb AS Raison, SUM(minutesarrets) AS Minutes FROM rapport_production_salles_blanches_2_repeat t1) 
    UNION ALL 
    (SELECT redresseuseminutesarrets AS Raison, SUM(minutesarretsredresseuse) AS Minutes FROM rapport_production_salles_blanches_3_repeat t2)
) t1 
UNION All 
(SELECT raisonarretsconvoyeurair AS Raison, SUM(minutesarretsconvoyeurair) AS Minutes FROM rapport_production_salles_blanches_4_repeat t3) 
) AS t123 GROUP BY t123.Raison ORDER BY t123.Minutes DESC