Copying records in MySQL

by Nazly on Thursday, 17th May 2007 11:42:05

Copying records from one table to another can be a very basic requirement. But writing queries to perform this task can be bit of a work around. But there is a very simple query to get this done.

Its by using INSERT … SELECT

http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

Copy one record from a table to another

1
INSERT INTO `dest_table` SELECT * FROM source_table WHERE id = '10'

It can be very simple as this. Even multiple records can be copied from a single table or several tables. If I'm not mistaken INSERT … SELECT works on MySQL versions 4.1 and above.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • FriendFeed
  • Google Bookmarks
  • email
  • LinkedIn
  • PDF
  • Print
  • Reddit

2 comments

Insert selects work on MySQL 3.23 and over.

They also work in other databases (oracle and postgres for sure)

by drac on May 17, 2007 at 20:17. #

Thanks for the info.. Didn't know that it worked with other DBs as well

by Nazly on May 17, 2007 at 21:48. #

Leave your comment

Required.

Required. Not published.

If you have one.