- Forums
- MySQL
- Mysql Copy Data From One Row And Insert Into Another
in this page i will show you example of how you can copy data and move it from one row in a different table to another row in another table using mysql command query [933], Last Updated: Sat May 18, 2024 
 
 wallpaperama
 Mon Oct 03, 2011 
 0 Comments
 1492 Visits
i was upgrading a blog for my friend and part of the upgrade required that some of the data be moved to another table, but the row names were not the same so i need to some how copy a value from each row and move it to the new table. after playing around this is what i was able to do.
lets say for example, i have two tables, 
tes1 and 
test2
if you need to follow along, here is the mysql dump if you want to follow:
DROP TABLE IF EXISTS `test1`;
CREATE TABLE `test1` (
  `id1` int(11) NOT NULL auto_increment,
  `name1` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id1`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
-- 
-- Dumping data for table `test1`
--
DROP TABLE IF EXISTS `test2`;
CREATE TABLE `test2` (
  `id2` int(11) NOT NULL auto_increment,
  `name2` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id2`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- www.wallpaperama.example
-- Dumping data for table `test2`
-- 
so lets say i want to move the value of 
name1 in the table 
tes1 to 
name2in the table 
tes2
as you can see from the mysqldump above, i have the value of 
name1 as 
wallpaperama so with the following mysql query i am going to copy the value of 
name1 and put it in the 
name2 field. so here is the example:
INSERT INTO test2 SET name2 = ( SELECT name1 FROM test1 WHERE id1 =1 ) 
so now when you go to the 
name2 field in the 
test2 table, you will see 
wallpaperama
hope that helps