COPY THE CONTENT OF ONE TABLE TO ANOTHER WITHIN THE SAME DATABASE

******** IF YOU HAVE CREATED THE TARGET TABLE ********

mysql>INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE;

******** IF THE TABLES HAVE DIFFERENT STRUCTURES USE THIS: ********

mysql>INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
mysql>INSERT INTO TARGET_TABLE (`col1_`,`col2_`) SELECT `col1`,`col2` FROM SOURCE_TABLE WHERE `foobar`=1

******** IF THE TABLE DOESN’T EXIST ********

mysql>CREATE TABLE table2 LIKE table1;

******** THEN COPY THE DATA OVER ********

mysql>INSERT INTO table2 SELECT * FROM table1

******** IF LARGE DATA DUMP THE FILE AND LOAD IT ********

mysql>CREATE TABLE table2 LIKE table1;
mysql>SELECT * INTO OUTFILE '/tmp/table1.txt' FROM table1;
mysql>LOAD DATA INFILE '/tmp/table1.txt' INTO TABLE table2;

NOTE: The ‘tmp/table1.txt’ is the PATH of or directory of the file.
******** If you want to create and copy the content in a single COMMAND, just use the SELECT: ********

mysql>CREATE TABLE new_tbl SELECT * FROM orig_tbl;

SOURCE:
http://stackoverflow.com/questions/2343644/in-mysql-how-to-copy-the-content-of-one-table-to-another-table-within-the-same

Advertisements
This entry was posted in MYSQL. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s