是一种常见的数据处理操作,可以通过使用UNION ALL和CASE WHEN语句来实现。
具体步骤如下:
SELECT 'A' AS col_name, A AS value FROM your_table
UNION ALL
SELECT 'B' AS col_name, B AS value FROM your_table
UNION ALL
SELECT 'C' AS col_name, C AS value FROM your_table
SELECT
CASE WHEN col_name = 'A' THEN value END AS transposed_column_A,
CASE WHEN col_name = 'B' THEN value END AS transposed_column_B,
CASE WHEN col_name = 'C' THEN value END AS transposed_column_C
FROM (
SELECT 'A' AS col_name, A AS value FROM your_table
UNION ALL
SELECT 'B' AS col_name, B AS value FROM your_table
UNION ALL
SELECT 'C' AS col_name, C AS value FROM your_table
)
这样,每列的值就会被转置为相应的行,每行包含了所有列的转置值。
推荐腾讯云的相关产品:云数据库 TencentDB for MySQL(链接:https://cloud.tencent.com/product/cdb),它提供了稳定可靠的云数据库服务,适用于各种规模的应用场景。
领取专属 10元无门槛券
手把手带您无忧上云