How to copy record from one table to another?
by Sanju[ Edit ] 2008-07-03 18:17:16
We can copy records from one table to another using
INSERT....SELECT
SYNTAX:
INSERT INTO tbl_name [(col_name,...)]SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
|
Example:
I have table test1 with:
id Type Name
1 A Ram
2 B Raj
3 C San
I want to create a table with inserting the records from the table test1.
mysql> CREATE TABLE test2(id int(11) PRIMARY KEY auto_increment, Name VARCHAR(20));
|
mysql> INSERT INTO test2(Name)SELECT Name FROM test1;
|
The above query helps to copy the record Name from the table test1 to test2.
|
mysql> SELECT * FROM test2;
|
id Name
1 Ram
2 Raj
3 San
|