原数据:
1
效果:
SQL:
-- 实现语句(不需要存储过程)
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'Max(IF(a.detection_name = ''',
a.detection_name,
''', a.end_value, null)) AS ''',
a.detection_name, ''''
)
) INTO @sql
FROM detection_result a;
SET @sql = CONCAT('Select order_code,', @sql,
' From detection_result a Group by a.order_code' );
PREPARE stmt FROM @sql; -- 动态生成脚本
EXECUTE stmt; -- 动态执行脚本
DEALLOCATE PREPARE stmt;
2
效果:
SQL:
SELECT order_code,GROUP_CONCAT(`detection_name`, ": (", end_value, "", result_value, ")")AS 结果 FROM detection_result
GROUP BY order_code