I new to MySQL. I would like to copy the content of one table to another table within the same database. Basically, I would like to insert to a table from another table. Is there easy way of doing this?
thanks for any help
I new to MySQL. I would like to copy the content of one table to another table within the same database. Basically, I would like to insert to a table from another table. Is there easy way of doing this?
thanks for any help
INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE
EDIT: or if the tables have different structures you can also:
INSERT INTO TARGET_TABLE (col1,col2) SELECT col1,col2 FROM SOURCE_TABLE
If the table doesn't exist, you can create one with the same schema like so:
CREATE TABLE table2 LIKE table1;
Then, to copy the data over:
INSERT INTO table2 SELECT * FROM table1
If table1 is large and you don't want to lock it for the duration of the copy process, you can do a dump-and-load instead:
CREATE TABLE table2 LIKE table1;
SELECT * INTO OUTFILE '/tmp/table1.txt' FROM table1;
LOAD DATA INFILE '/tmp/table1.txt' INTO TABLE table2;
If you want to create and copy the content in a single shot, just use the SELECT:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;