我有一张动态形成的表格。例如,表data_table15如下,
Id Cost Product Quantity
1 3216 Test17 17
2 3217 Test18 18
执行此查询时,表示上表,
SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT('select id, ' '''', COLUMN_NAME, ''' col, ', column_name, ' as value from data_table15') separator ' union all ' ) INTO @sql FROM INFORMATION_SCHEMA.COLUMNS where table_name = 'data_table15' and column_name <> 'id';
set @sql = CONCAT(@sql,' order by 1,2');
PREPARE stmt FROM @sql;
EXECUTE stmt;
结果是,
1 Product Test17
1 Cost 3216
1 Quantity 17
2 Product Test18
2 Cost 3217
2 Quantity 18
但我期待这样的结果,
Id 1 2
Product Test17 Test18
Cost 3216 3217
Quantity 17 18
如何更改查询以实现此目的?