在MySQL中,可以使用以下方式將行轉(zhuǎn)列:
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;
這些語句假設(shè)你有一個表名為your_table
,包含id、key和value列。你需要根據(jù)實際的表結(jié)構(gòu)和數(shù)據(jù)進行調(diào)整。