我想转置mysql行到列使用动态查询我完成了查询与预期的结果。查询是在我的localhost PhpMyAdmin版本5.2.0没有任何错误,但我得到一个语法错误在其他PhpMyAdmin版本。
SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT( 'SUM( CASE WHEN product_code = "', product_code, '" THEN available_quantity ELSE 0 END) AS `', product_code,'`') )
INTO @sql FROM west_stock_details;
SET @sql = CONCAT('SELECT ', @sql, ', SUM(available_quantity) as TOTAL FROM west_stock_details where consignee_name IN ("PARKSON PACKAGING LTD.", "PARKSONS PACKAGING LIMITED", "PARKSONS PACKAGING LIMITED.", "PARKSONS PACKAGING LTD.", "PARKSONS PACKAGING LTD.(PUNE)") and stor_loc_desc NOT IN ("BCM PG6 SL WH", "Quality HOLD Mat", "BCM PG5 MFS WH", "BCM PG4 MFS WH", "BCM PG7 MFS WH", "BCM PG4 SL WH", "BCM PG7 SL WH", "Damaged Stocks", "BCM PM1A MFS WH", "Bad Quality Matl", "MPBC-Bad quality", "BCM PG6 SL WH", "BCM PG7 SL WH") and storage_location NOT IN ("T400","BSL6","BC15","BC14","BC17","BSL4","BSL7","DS01","BC1A","BC04","BE06","BSL7","BSL","BCIC","MPSL","UVSL","T203","BSLA") and storage_location not like "%SL%" GROUP BY destination');
SELECT @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;
错误为:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' SUM(available_quantity) as TOTAL FROM west_stock_details where consignee_name I' at line 1
.
下面是dbfiddle链接:是的。
有任何建议或帮助,将不胜感激。
1条答案
按热度按时间dw1jzc5e1#
这必须解决您的问题。
fiddle