在MySQL中,可以使用以下方式將行轉列:
SELECT
id,
MAX(CASE WHEN key = 'key1' THEN value END) AS value1,
MAX(CASE WHEN key = 'key2' THEN value END) AS value2,
MAX(CASE WHEN key = 'key3' THEN value END) AS value3
FROM
your_table
GROUP BY
id;
SELECT
id,
value1,
value2,
value3
FROM
your_table
PIVOT (
MAX(value)
FOR key IN (key1, key2, key3)
) AS p;
這些語句假設你有一個表名為your_table
,包含id、key和value列。你需要根據實際的表結構和數據進行調整。