Export a single row from a mysql table

While it doesn't happen that often, occasionally I'll need to export a certain subset of data from a MySQL table instead of the entire dataset. You can do this with mysqldump using the --where option which allows you to apply a WHERE clause that filters the data in your tables based on a certain condition.

Let's say in Drupal, you'd like to copy a single comment from one table into another. Here's how you'd get an insert statement for comment ID 55:

mysqldump -u user -p --where="cid=55" dbname table_name
 
LOCK TABLES `comments` WRITE;
INSERT INTO `comments` VALUES (4,0,2748,5,'this is a comment dude.','This is a comment.','216.555.252.21',1271092388,1,1,'01/','david smith','','');
UNLOCK TABLES;

Now you can run this INSERT statement on the database you'd like to copy this record to, and you're done.

Tags:

Comments

Nice Script :)

Nice script - just blogged a slight improvement to it here: http://www.thingy-ma-jig.co.uk/blog/11-07-2011/export-a-single-row-from-...

Useful script, thanks!

Useful script, thanks!