移动mysql中的行和列

bwntbbo3  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(165)

我需要使用sql更改表的行和列,而不使用sqlserver中的任何指针或任何其他函数。前任:
我把这些数据放在table上:

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

有什么解决办法吗?

qyyhg6bp

qyyhg6bp1#

这应该起作用:

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]
rt4zxlrg

rt4zxlrg2#

像这样的

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
okxuctiv

okxuctiv3#

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

相关问题