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!

google

power

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <strike> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <css>, <drupal5>, <drupal6>, <javascript>, <mysql>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.