MYSQL:将行连接为列

时间:2015-05-21 13:17:15

标签: mysql join

我正在使用 MySQL ,并尝试对包含多行数据的表进行排序/连接,以便每年的数据都在相邻列中表示,如果数据不存在,它表示为null。

+------+------+-----------+ | code | year | enquiries | +------+------+-----------+ | 1 | 2012 | 302 | | 2 | 2012 | 274 | | 3 | 2012 | 288 | | 4 | 2012 | 301 | | 5 | 2012 | 192 | | 1 | 2013 | 406 | | 3 | 2013 | 297 | | 4 | 2013 | 199 | | 1 | 2014 | 254 | | 2 | 2014 | 396 | | 3 | 2014 | 187 | | 4 | 2014 | 213 | | 5 | 2014 | 316 | | 6 | 2014 | 222 | +------+------+-----------+

以便上表如下所示:

+------+------+-----------+------+-----------+------+-----------+ | code | year | enquiries | year | enquiries | year | enquiries | +------+------+-----------+------+-----------+------+-----------+ | 1 | 2012 | 302 | 2013 | 406 | 2014 | 254 | | 2 | 2012 | 274 | NULL | NULL | NULL | NULL | | 3 | 2012 | 288 | 2013 | 297 | 2014 | 187 | | 4 | 2012 | 301 | 2013 | 199 | 2014 | 213 | | 5 | 2012 | 192 | NULL | NULL | 2014 | 316 | | 6 | NULL | NULL | NULL | NULL | 2014 | 222 | +------+------+-----------+------+-----------+------+-----------+

我认为我应该寻找的是全外连接,但使用MySQL是不可能的。

我设法使用多个 LEFT OUTER JOINS 将多个行连接在一起,但这省略了所有3年内数据都不存在的行。

2 个答案:

答案 0 :(得分:1)

对于您展示的有限数量的记录, 你可以写一个简单的数据透视解决方案,如下

但是,如果年数增加,则必须生成此查询运行时并执行。

mysql> select
    ->        code
    ->      , max( case when year=2012 then year end ) as y2012
    ->      , max( case when year=2012 then enquiries end ) as enq_2012
    ->      , max( case when year=2013 then year end ) as y2013
    ->      , max( case when year=2013 then enquiries end ) as enq_2013
    ->      , max( case when year=2014 then year end ) as y2014
    ->      , max( case when year=2014 then enquiries end ) as enq_2014
    ->   from so_20150521.so_q30374911
    ->  group by code
    ->  order by code;
+------+-------+----------+-------+----------+-------+----------+
| code | y2012 | enq_2012 | y2013 | enq_2013 | y2014 | enq_2014 |
+------+-------+----------+-------+----------+-------+----------+
|    1 |  2012 |      302 |  2013 |      406 |  2014 |      254 |
|    2 |  2012 |      274 |  NULL |     NULL |  2014 |      396 |
|    3 |  2012 |      288 |  2013 |      297 |  2014 |      187 |
|    4 |  2012 |      301 |  2013 |      199 |  2014 |      213 |
|    5 |  2012 |      192 |  NULL |     NULL |  2014 |      316 |
|    6 |  NULL |     NULL |  NULL |     NULL |  2014 |      222 |
+------+-------+----------+-------+----------+-------+----------+
6 rows in set (0.02 sec)

http://sqlfiddle.com/#!9/4a46d/2

上的SQL小提琴演示

进一步简化可以是:

mysql> select
    ->        code
    ->      , max( case when year=2012 then enquiries end ) as enq_2012
    ->      , max( case when year=2013 then enquiries end ) as enq_2013
    ->      , max( case when year=2014 then enquiries end ) as enq_2014
    ->   from so_20150521.so_q30374911
    ->  group by code
    ->  order by code;
+------+----------+----------+----------+
| code | enq_2012 | enq_2013 | enq_2014 |
+------+----------+----------+----------+
|    1 |      302 |      406 |      254 |
|    2 |      274 |     NULL |      396 |
|    3 |      288 |      297 |      187 |
|    4 |      301 |      199 |      213 |
|    5 |      192 |     NULL |      316 |
|    6 |     NULL |     NULL |      222 |
+------+----------+----------+----------+
6 rows in set (0.00 sec)

答案 1 :(得分:0)

试试这个:

  

(SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id)UNION(SELECT * FROM t1   RIGHT JOIN t2 ON t1.id = t2.id)