在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列。你需要根据实际的表结构和数据进行调整。