我在我安装的 MySQL 版本上遵循本教程,但它给我一个错误:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(property_name = ''',
property_name,
''', value, NULL)) AS ',
property_name
)
) INTO @sql
FROM
properties;
SET @sql = CONCAT('SELECT item_id, ', @sql, ' FROM properties GROUP BY item_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
我将它粘贴到 phpMyAdmin 的 SQL 编辑器上。
我听从了建议。没有显示错误,但结果如下:
SELECT item_id
,MAX(IF(property_name = 'color', value, NULL)) AS color
,MAX(IF(property_name = 'size', value, NULL)) AS size
,MAX(IF(property_name = 'weight', value, NULL)) AS weight
FROM properties GROUP BY item_id