在MySQL中,可以使用UNION操作符来实现列转行。
例如,假设有以下表格:
+----+-------+| id | value |+----+-------+| 1 | 10 || 2 | 20 || 3 | 30 |+----+-------+要将该表格中的value列转换为行,可以使用以下查询:
SELECT 'value' AS type, value FROM table_nameUNIONSELECT 'id' AS type, CAST(id AS CHAR) AS value FROM table_name;查询结果如下:
+-------+-------+| type | value |+-------+-------+| value | 10 || value | 20 || value | 30 || id | 1 || id | 2 || id | 3 |+-------+-------+在上述查询中,第一个SELECT语句将value列查询为type为’value’的行,第二个SELECT语句将id列查询为type为’id’的行。最后,使用UNION将两个结果集合并为一个结果集。

