基于两个列,将行动态转换为列的Mysql查询

8

我将按照以下问题中的方法,使用MySQL查询动态地将行转换为列。这个方法很好用,但是我需要基于两列来进行转换,

上述链接中提到的查询仅适用于单个名为"data"的列,但我想要使用两个列"data"和"price"。

我在这里添加了一个例子,

给定一个名为A的表,它看起来像:

Table A

|  id|order|data|item|Price|
-----+-----+----------------
|   1|    1|   P| 1  | 50  |
|   1|    1|   P| 2  | 60  |
|   1|    1|   P| 3  | 70  |
|   1|    2|   Q| 1  | 50  |
|   1|    2|   Q| 2  | 60  |
|   1|    2|   Q| 3  | 70  |
|   2|    1|   P| 1  | 50  |
|   2|    1|   P| 2  | 60  |
|   2|    1|   P| 4  | 80  |
|   2|    3|   S| 1  | 50  |
|   2|    3|   S| 2  | 60  |
|   2|    3|   S| 4  | 80  |

我希望写一个像以下这样的查询:

Result Table

|  id|order1|order2|order3|item1|item2|item3|item4|
-----+-----+---------------------------------------
|   1|    P |    Q |      | 50  | 60  | 70  |     |
|   2|    P |      |    S | 50  | 60  |     | 80  |

我尝试创建了两个不同的查询,然后使用联接来实现此目的,但这可能不是一个好的解决方案。有没有人能够提供与上面链接中提到的相同的解决方案呢?
谢谢。
1个回答

19

假设您已知道orderitem的特定数量,那么您可以将查询硬编码为:

select id,
  max(case when `order` = 1 then data end) order1,
  max(case when `order` = 2 then data end) order2,
  max(case when `order` = 3 then data end) order3,
  max(case when item = 1 then price end) item1,
  max(case when item = 2 then price end) item2,
  max(case when item = 3 then price end) item3,
  max(case when item = 4 then price end) item4
from tableA
group by id;

请查看演示。但你会遇到的部分问题是因为你正在尝试转换多个数据列。我的建议是先对数据进行解聚以获得最终结果。MySQL没有解聚函数,但可以使用UNION ALL将多个列对转换成行。解聚代码类似于以下内容:

select id, concat('order', `order`) col,  data value
from tableA
union all
select id, concat('item', item) col, price value
from tableA;

请查看演示。这将会得到以下结果:

| ID |    COL | VALUE |
-----------------------
|  1 | order1 |     P |
|  1 | order1 |     P |
|  1 | order1 |     P |
|  1 |  item1 |    50 |
|  1 |  item2 |    60 |
|  1 |  item3 |    70 |

你可以看到这里使用了 order/dataitem/price 的多列,并将其转换为多行。完成后,您可以使用带有 CASE 的聚合函数将值转换回列:

select id, 
  max(case when col = 'order1' then value end) order1,
  max(case when col = 'order2' then value end) order2,
  max(case when col = 'order3' then value end) order3,
  max(case when col = 'item1' then value end) item1,
  max(case when col = 'item2' then value end) item2,
  max(case when col = 'item3' then value end) item3
from
(
  select id, concat('order', `order`) col,  data value
  from tableA
  union all
  select id, concat('item', item) col, price value
  from tableA
) d
group by id;

请查看 演示。最后,您需要将上述代码转换为动态准备语句查询:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when col = ''',
      col,
      ''' then value end) as `', 
      col, '`')
  ) INTO @sql
FROM
(
  select concat('order', `order`) col
  from tableA
  union all
  select concat('item', `item`) col
  from tableA
)d;

SET @sql = CONCAT('SELECT id, ', @sql, ' 
                  from
                  (
                    select id, concat(''order'', `order`) col,  data value
                    from tableA
                    union all
                    select id, concat(''item'', item) col, price value
                    from tableA
                  ) d
                  group by id');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

请参见带有演示的SQL Fiddle。这将给出一个结果:

| ID | ORDER1 | ORDER2 | ORDER3 | ITEM1 | ITEM2 |  ITEM3 |  ITEM4 |
-------------------------------------------------------------------
|  1 |      P |      Q | (null) |    50 |    60 |     70 | (null) |
|  2 |      P | (null) |      S |    50 |    60 | (null) |     80 |

如果我有两个表:列表和信息表,当我尝试修改查询时,为什么只显示一行而不是所有信息? - Chris Sum

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接