我在我安装的MySQL版本上关注这个tutorial,但它给我一个错误:
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
解决方法:
如果你有权访问MySQL命令行,我想你会发现你的SQL代码没问题(只要@sql不等于NULL),问题就在于phpMyAdmin.另一个想法是将代码包装在存储过程中,然后调用该过程.
标签:mysql,sql
来源: https://codeday.me/bug/20190529/1178664.html