mysql union和group by similar fields

时间:2012-06-19 15:01:07

标签: mysql group-by union alias

这是我的查询(我用通用名称替换了表名)。我试图在两个不同的查询上进行联合,以便按日期对它们进行分组,以便具有相似日期的结果作为一行出现。

尝试执行时,我得到“每个派生表必须有自己的别名”错误。我输错了什么?

我研究了这个,但找不到答案。每个选定的字段都有别名?或者是第一个SELECT中的问题?

SELECT SUM(val), id, dat, title FROM (

                      SELECT table1.product_id as id, SUM(table1.qty) as val, DATE_FORMAT(table1.created, '%Y-%m-1') as dat, table2.title as title 
                      FROM table1
                      LEFT JOIN table3 ON table1.event_id = table3.id
                      LEFT JOIN table2 ON table1.product_id = table2.id
                      WHERE table1.user_id = $user_id AND table3.active != 2 AND table3.temp = 0 AND table2.active != 2

                      GROUP BY dat

                      UNION ALL

                      SELECT table4.product_id as id, SUM(table4.qty) as val, DATE_FORMAT(table4.created, '%Y-%m-1') as dat, table2.title as title 
                      FROM table4
                      LEFT JOIN table5 ON table4.festival_id = table5.id
                      LEFT JOIN table2 ON table4.product_id = table2.id
                      WHERE table4.user_id = $user_id AND table5.active != 2 AND table2.active != 2

                      GROUP BY dat

                      )
                      GROUP BY id
                      ORDER BY dat ASC

这是我试图做的事情:

我原来的结果:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [val] => 1
        [dat] => 2012-05-1
        [title] => Test Product
    )

[1] => stdClass Object
    (
        [id] => 1
        [val] => 8
        [dat] => 2012-06-1
        [title] => Test Product
    )

[2] => stdClass Object
    (
        [id] => 2
        [val] => 4
        [dat] => 2012-06-1
        [title] => Test Product 2
    )

[3] => stdClass Object
    (
        [id] => 3
        [val] => 6
        [dat] => 2012-06-1
        [title] => Test Product 3
    )

[4] => stdClass Object
    (
        [id] => 1
        [val] => 10
        [dat] => 2012-05-1
        [title] => Test Product
    )

[5] => stdClass Object
    (
        [id] => 1
        [val] => 8
        [dat] => 2012-06-1
        [title] => Test Product
    )

[6] => stdClass Object
    (
        [id] => 2
        [val] => 3
        [dat] => 2012-06-1
        [title] => Test Product 2
    )

[7] => stdClass Object
    (
        [id] => 3
        [val] => 3
        [dat] => 2012-06-1
        [title] => Test Product 3
    )

)

因此,如果他们有相似的日期和ID,我需要这些只是一个结果。像这样:

    Array
(
[0] => stdClass Object
    (
        [id] => 1
        [val] => 11
        [dat] => 2012-05-1
        [title] => Test Product
    )

[1] => stdClass Object
    (
        [id] => 1
        [val] => 8
        [dat] => 2012-06-1
        [title] => Test Product
    )

[2] => stdClass Object
    (
        [id] => 2
        [val] => 7
        [dat] => 2012-06-1
        [title] => Test Product 2
    )

[3] => stdClass Object
    (
        [id] => 3
        [val] => 9
        [dat] => 2012-06-1
        [title] => Test Product 3
    )

)

如果您还有其他需要,请告诉我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT SUM(val), id, dat, title FROM (

                  SELECT table1.product_id as id, SUM(table1.qty) as val, DATE_FORMAT(table1.created, '%Y-%m-1') as dat, table2.title as title 
                  FROM table1
                  LEFT JOIN table3 ON table1.event_id = table3.id
                  LEFT JOIN table2 ON table1.product_id = table2.id
                  WHERE table1.user_id = $user_id AND table3.active != 2 AND table3.temp = 0 AND table2.active != 2

                  GROUP BY dat

                  UNION ALL

                  SELECT table4.product_id as id, SUM(table4.qty) as val, DATE_FORMAT(table4.created, '%Y-%m-1') as dat, table2.title as title 
                  FROM table4
                  LEFT JOIN table5 ON table4.festival_id = table5.id
                  LEFT JOIN table2 ON table4.product_id = table2.id
                  WHERE table4.user_id = $user_id AND table5.active != 2 AND table2.active != 2

                  GROUP BY dat

                  ) AS t
                  GROUP BY id, dat
                  ORDER BY dat ASC

如错误所示,每个视图/派生表都必须有一个别名..

编辑:这将为您提供具有不同ID / dat对的记录。似乎这就是你追求的目标。