我想将以下数据库表从动态列转换为MySQL中的行:
我已经深入了解了stackoverflow.com以及这个很好的例子here,但没有一个案例符合我的要求。
上面链接中显示的示例将符合我的要求,如果它是动态的(我不知道值'颜色'或'大小'因为它动态变化):
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;
所以这是我的数据库表:
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
现在我的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
答案 0 :(得分:0)
使用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