在mysql中移动行和列

时间:2018-09-04 06:22:30

标签: mysql sql

我需要使用sql更改表行和列,而无需在Sqlserver或任何其他函数中使用任何指针。例如:

我在表上有这些数据:

h1 | h2 | h3 | date                     
1    2    3     d1  
4    5    6     D2  

我要替换它们并转换为此:

h value | date
1          d1
2          d1
3          d1
4          D2
5          D2
6          D2

有解决方案吗?

3 个答案:

答案 0 :(得分:1)

这应该有效:

SELECT h1 AS h_value, date FROM [table] UNION ALL
SELECT h2 AS h_value, date FROM [table] UNION ALL
SELECT h3 AS h_value, date FROM [table]

答案 1 :(得分:1)

SELECT h_value, date
FROM
(    
SELECT h1 AS h_value, date
    FROM your_table

    UNION

    SELECT h2 AS h_value, date
    FROM your_table

    UNION

    SELECT h3 AS h_value, date
    FROM your_table
)
ORDER BY h_value

答案 2 :(得分:0)

类似

create table table_new as 
select h1 as "h value",date from table
union all
select h2,date from table
union all
select h3,date from table