简体   繁体   English

将数据从一个表复制到另一个表

[英]copy data from one tables to another one

I have two table OldDocuments and Documents and I want to import Data from one table, OldDocument and copy it into the new table knowing that the two tables do not have the same column name numbers neither the same names. 我有两个表OldDocumentsDocuments ,我想从一个表OldDocument中导入数据,并将其复制到新表中,因为这两个表没有相同的列名编号,也没有相同的名称。

Here are the columns I want to import to the new table 这是我要导入到新表中的列

OldDocument 旧文件

Id(PK) ID(PK)
Document(BLOB) 文件(BLOB)
fileName(VARCHAR) fileName(VARCHAR)
DocumentType(VARCHAR) DocumentType(VARCHAR)
user_Id(FK) user_Id(FK)

Document 文献

Id (PK) ID(PK)
Document_content (BLOB) Document_content(BLOB)
fileName (VARCHAR) fileName(VARCHAR)
DocType(VARCHAR) DocType(VARCHAR)
user_Id (FK) user_Id (FK)

I need a query that will select from one table and copy into the new tables these columns. 我需要一个查询,该查询将从一个表中选择并将这些列复制到新表中。 Something like 就像是

    INSERT INTO DOCUMENT(ID,document_content, fileName , DocType, user_Id) 
    VALUES (get data from the old table)

You can insert a result from another select. 您可以插入另一个选择的结果。 Here is the doc: http://www.w3schools.com/sql/sql_insert_into_select.asp 这是文档: http : //www.w3schools.com/sql/sql_insert_into_select.asp

Ex: INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1; 例如: INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;

INSERT INTO Document (Id,Document_content,fileName,DocType,user_Id)
SELECT Id,Document,fileName,DocumentType,user_Id
FROM OldDocument
;

Use INSERT INTO ... SELECT : 使用INSERT INTO ... SELECT

INSERT INTO DOCUMENT(ID,document_content, fileName , DocType, user_Id) 
SELECT ID, Document, fileName, DocumentType, user_id FROM OldDocument;
INSERT INTO Document
SELECT Id,Document,fileName,DocumentType,user_Id
FROM OldDocument

Try using INSERT INTO ... SELECT : 尝试使用INSERT INTO ... SELECT

http://dev.mysql.com/doc/refman/5.0/en/insert-select.html http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

INSERT INTO Document (ID, document_content, fileName , DocType, user_Id) 
SELECT Id, Document, filename, DocumentType, user_Id FROM OldDocument

try this: 尝试这个:

INSERT INTO DOCUMENT(ID,document_content, fileName , DocType, user_Id) 
    select Id, Document, filename, DocumentType, user_Id from OldDocument

This is the general format for this type of problems correct answer 这是这类问题的一般格式正确答案

INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

Source: http://www.w3schools.com/sql/sql_insert_into_select.asp 资料来源: http : //www.w3schools.com/sql/sql_insert_into_select.asp

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

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