[英]MySQL PivotTable - How to convert dynamic columns to rows?
I would like to transform the following database table from dynamic columns to rows in MySQL: 我想将以下数据库表从动态列转换为MySQL中的行:
I already took a deep look at stackoverflow.com as well as at this great example here but none of the cases matches my requirement. 我已经对stackoverflow.com以及此处的示例进行了深入研究,但是没有一种情况符合我的要求。
The example shown from the link above would match my requirements if it would be dynamic (I do not know the value 'color' or 'size' because it changes dynamically): 如果上面的示例是动态的,则上面的链接所示的示例将符合我的要求(我不知道值'color'或'size',因为它是动态变化的):
SELECT
item_id,
MAX(IF(property_name = 'color', value, NULL)) AS color,
MAX(IF(property_name = 'size', value, NULL)) AS size,
...
...
...
FROM
properties
GROUP BY
item_id;
So this is my Database Table: 这是我的数据库表:
id | customer_id | customer_tbl_id | customer_tbl_col_name
1 | 1 | 1 | CustomerColName_1
2 | 1 | 1 | CustomerColName_2
3 | 1 | 1 | CustomerColName_3
4 | 1 | 2 | CustomerColName_4
5 | 1 | 2 | CustomerColName_5
6 | 2 | 1 | CustomerColName_6
7 | 2 | 1 | CustomerColName_7
Now the result of my SQL query should be like this: 现在,我的SQL查询结果应如下所示:
1 (customer_id) | 1 (customer_tbl_id) | CustomerColName_1 | CustomerColName_2 | CustomerColName_3
1 (customer_id) | 2 (customer_tbl_id) | CustomerColName_4 | CustomerColName_5
2 (customer_id) | 1 (customer_tbl_id) | CustomerColName_6 | CustomerColName_7
Use GROUP_CONCAT 使用GROUP_CONCAT
SELECT customer_id, customer_tbl_id, GROUP_CONCAT(customer_tbl_col_name) as customer_tbl_col_name
FROM table
GROUP BY customer_id , customer_tbl_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.