在MySQL中,行轉列和列轉行通常使用以下方法來實現:
SELECT
MAX(CASE WHEN attribute = 'attribute1' THEN value END) AS attribute1,
MAX(CASE WHEN attribute = 'attribute2' THEN value END) AS attribute2,
MAX(CASE WHEN attribute = 'attribute3' THEN value END) AS attribute3
FROM your_table
GROUP BY id;
SELECT 'attribute1' AS attribute, attribute1 AS value FROM your_table
UNION ALL
SELECT 'attribute2' AS attribute, attribute2 AS value FROM your_table
UNION ALL
SELECT 'attribute3' AS attribute, attribute3 AS value FROM your_table;
這些方法可以根據具體的需求和數據結構進行調整和擴展,實現行轉列和列轉行的操作。