anyavacy
anyavacy

Reputation: 1697

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.

Here are the columns I want to import to the new table

OldDocument

Id(PK)
Document(BLOB)
fileName(VARCHAR)
DocumentType(VARCHAR)
user_Id(FK)

Document

Id (PK)
Document_content (BLOB)
fileName (VARCHAR)
DocType(VARCHAR)
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)

Upvotes: 0

Views: 52

Answers (7)

anyavacy
anyavacy

Reputation: 1697

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

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 1549

try this:

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

Upvotes: 1

Jazi
Jazi

Reputation: 6712

Try using INSERT INTO ... SELECT:

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

Upvotes: 1

Dhaval
Dhaval

Reputation: 2379

INSERT INTO Document
SELECT Id,Document,fileName,DocumentType,user_Id
FROM OldDocument

Upvotes: 1

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79889

Use INSERT INTO ... SELECT:

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

Upvotes: 1

Nebojsa Susic
Nebojsa Susic

Reputation: 1260

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

Upvotes: 1

naoxink
naoxink

Reputation: 597

You can insert a result from another select. Here is the doc: http://www.w3schools.com/sql/sql_insert_into_select.asp

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

Upvotes: 1

Related Questions