写一段mySQL代码:将一行多列数据转换为一列多行,没有ID
时间: 2024-03-05 17:55:15 浏览: 55
假设你有一张名为`table_name`的表,其中有三列`column1`、`column2`和`column3`。可以使用如下MySQL代码将一行多列数据转换为一列多行:
```
SELECT
column1 AS new_column
FROM
table_name
UNION ALL
SELECT
column2 AS new_column
FROM
table_name
UNION ALL
SELECT
column3 AS new_column
FROM
table_name;
```
这将返回一个名为`new_column`的新列,其中包含所有行的`column1`、`column2`和`column3`值,每个值都在单独的行中。`UNION ALL`用于将三个`SELECT`语句的结果合并为一个结果集。如果你想按照某种特定的顺序返回结果,请在每个`SELECT`语句中使用`ORDER BY`子句。例如,按照`column1`、`column2`和`column3`的顺序返回结果:
```
SELECT
column1 AS new_column
FROM
table_name
UNION ALL
SELECT
column2 AS new_column
FROM
table_name
UNION ALL
SELECT
column3 AS new_column
FROM
table_name
ORDER BY
column1, column2, column3;
```
阅读全文