SET @@group_concat_max_len = 5000;
SET @sql = NULL;
SELECT
@sql = GROUP_CONCAT(DISTINCT
+(
' MAX(IF(cid = "', d.cid, '", `data`, NULL)) AS ', c.form_key
)
)
FROM
webform_submitted_data AS d
INNER JOIN webform_component AS c ON c.cid = d.cid AND d.nid = c.nid;
SET @sql =
CONCAT(
'SELECT *, ', @sql, '
FROM webform_submitted_data
GROUP BY sid ORDER BY nid, sid, cid'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;