简体   繁体   English

使用第一个表中的某些内容作为接收表的列,将数据从SQL中的一个表移动到另一个表中

[英]Move data from a table to another table in SQL using some of the first table content as columns of the receiving table

Sorry for the long title, but couldn't find something shorter unfortunately. 很抱歉,标题太长了,但很遗憾,找不到短的标题了。 So my problem is the following, I have been given a SQL table with the following columns: 所以我的问题是以下,我得到了一个带有以下各列的SQL表:

Id_String  |  Date  |  Time  |  Value

Id_String is a number that ranges from 101 to 120. Here is a sample of the data: Id_String是一个数字,范围从101到120。这是数据的示例:

101 1982-12-02  07:48:03    0.001
102 1982-12-02  07:48:04    0.002
102 1982-12-02  07:50:04    0.004
102 1982-12-02  07:52:04    0.006
103 1982-12-02  07:52:05    0.006

What I need to do is simple yet I'm not an expert so I'm not sure this can be done in an easy way. 我需要做的很简单,但是我不是专家,所以我不确定这可以通过简单的方式完成。 I'd like to build a table with the following columns: 我想建立一个包含以下各列的表:

Date  |  Time  |  Value_Id_String_101  |  Value_Id_String_102 | ... | Value_Id_String_120

That is, for each date and time I have all the values of the String Ids. 也就是说,对于每个日期和时间,我都具有字符串ID的所有值。 Is there a SQL command that I can run on the first table in order to automatically generate the second table? 我可以在第一个表上运行以自动生成第二个表的SQL命令吗? Or should I write a script that does it "manually"? 还是应该编写一个“手动”执行的脚本?

Thanks a lot. 非常感谢。

Use these query 使用这些查询

       INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;

    or

 CREATE TABLE newtable LIKE oldtable; INSERT newtable SELECT * FROM oldtable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM