gpt4 book ai didi

mysql - SQL查询从一个表的某些列复制到另一个表中的一列

转载 作者:行者123 更新时间:2023-11-29 03:53:52 25 4
gpt4 key购买 nike

我有这个场景

表01

reportID | response1 | response2 | response3 | response4 | response5
1 | aaa | bbb | ccc | ddd | eee
2 | fff | ggg | hhh | iii | jjj
3 | lll | mmm | nnn | ooo | ppp
...

我想将这些数据插入到表02中,结果应该是这样的

id | reportID | response
1 | 1 | aaa
2 | 1 | bbb
3 | 1 | ccc
4 | 1 | ddd
5 | 1 | eee
6 | 2 | fff
7 | 2 | ggg
8 | 2 | hhh
9 | 2 | iii
10 | 2 | jjj
11 | 3 | lll
...

我怎样才能做到这一点,我试过了:

INSERT INTO table02 (reported, response) 
SELECT reportid, reponse1 FROM table01

但是好像不太对。

表 01 包含大约 4k 行,因此 table 2 将有大约 20k。

这里最好的方法是什么。

我可以创建一个控制台应用程序并从那里执行,但是我想从 SQL Server Management Studio 执行。

最佳答案

最简单的方法是使用union all:

insert into table02(reported, response)
select reportid, reponse1 from table01 union all
select reportid, reponse2 from table01 union all
select reportid, reponse3 from table01 union all
select reportid, reponse4 from table01 union all
select reportid, reponse5 from table01;

关于mysql - SQL查询从一个表的某些列复制到另一个表中的一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28666516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com