MySQL将行与列相乘

时间:2017-06-27 23:48:12

标签: mysql pivot-table

https://i.stack.imgur.com/5Pw2L.png

我有一个问题,我想在MySQL中进行预处理。我有一个选择,返回id的多个行和列。我想将行转换为相同id的列,按照附图对它们进行分组。列名对我来说并不重要,因为我只需要每个id的值。

+---+---+---+---+-----+---+
| 1 | a | b | c | ... | x |
+---+---+---+---+-----+---+
| 1 | d | e | f | ... | y |
+---+---+---+---+-----+---+
| 2 | g | h | i | ... | z |
+---+---+---+---+-----+---+
| 2 | j | k | l | ... | q |
+---+---+---+---+-----+---+
| 3 | m | n | o | ... | w |
+---+---+---+---+-----+---+
| 3 | p | q | r | ... | t |
+---+---+---+---+-----+---+

+---+---+---+---+-----+---+---+---+---+-----+---+
| 1 | a | b | c | ... | x | d | e | f | ... | y |
+---+---+---+---+-----+---+---+---+---+-----+---+
| 2 | g | h | i | ... | z | j | k | l | ... | q |
+---+---+---+---+-----+---+---+---+---+-----+---+
| 3 | m | n | o | ... | w | p | q | r | ... | t |
+---+---+---+---+-----+---+---+---+---+-----+---+

1 个答案:

答案 0 :(得分:1)

不幸的是,如果表中有不同数量的id重复,则无法动态创建这样的列。 您可以使用group concat将相同的列放入一个逗号分隔的列

1266135 Or 1266197 Or 1266197 Or 1266197 Or 1266256 Or 1266256