Search code examples
mysqlsqlmysqlimport

Import into MySQL from SQL file, but only rows where field = value


Is there a way from /usr/bin/mysql or /usr/bin/mysqlimport to import a sql file but exclude all rows where a specific field doesn't have a specific value?

I want to restore data from backup, but only backup data where the field "origin" has the value "import". So I have a script that first deletes all rows in the live database "where origin = 'import'" and then I want to import from a file all those rows that I have backed up, but not every single row, since there are other origins in the backup.

Am I making myself clear? Any ideas?


Solution

  • Two thoughts:

    1. Prepare the sqlfile by adding a where clause to each statement. Should be relatively easy with your favorite scripting language.

    2. Import to a temp "schema" and for each table:

      insert into realtable 
      select * from temptable where origin = 'import';