Mysql – Repair table field: Import/append records from specific backup field if they are not null

MySQL

One of my 3rd party applications that writes to my production MySQL database mydb has nulled SOME of the data in one of my fields myfield in table mytable.

I'm trying to import only the NON null records for this field from my backup mydump.sql. The ID field can be used as a unique identifier to make the comparison.

Do I have to import the backup in it's entirety to a temp location, and then build a query/command? Or can I write a query that will also look into an external file? Either way I think the query is a bit above my skill level, so any help will be appreciated.

Ideally, if myfield data in my prod table exists (i.e. not null) then we'll leave that alone, as I'm only looking to import/append myfield data if it's in the backup and not in the prod field.

Best Answer

There are two solutions I can immediately think of for your current situation:

  1. Write a tool to parse the MySQL dump file, extract the records you're interested in, and insert them.
    This sounds like a lot of work, and a lot of time (especially if your dump is large).

  2. Do as you suggest in the question: Reimport the data somewhere and use a SQL query to get the fields you want.

My vote would be for (2) because you can be more precise about the selection of the rows (and the SQL will probably be faster than writing a script to parse the dump file. You'll need a machine running MySQL, with enough room to import your data, but you might be able to do that on your main server by restoring to an alternate database.