Dwix
Dwix

Reputation: 1257

mysqldump a database with condition on tables

My MySQL database has tables which contain the column company_id and other tables don't, and I want to dump all the data into a SQL file.

But I need to add a where company_id=1 only for the tables that have this column, and for the rest I need to get all the data.

This is not working for me because I have to specify the table name, and what I need is to dump everything with a the condition on every table that has that column :

mysqldump -u root -p root databaseName tableName --where company_id=1 > mydb_tables.sql

Upvotes: 0

Views: 3750

Answers (1)

Piemol
Piemol

Reputation: 896

Have you tried it with the combination of --tables option of mysqldump? https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#mysqldump-filter-options

You'll have to modify your mysqldump command each time you have more/less tables with that column name though.

Edit: You can try specifying the table names after the database name too, --tables is not really needed then.

Normally, mysqldump treats the first name argument on the command line as a database name and following names as table names.

Edit 2: I just tested this dumping multiple tables and their rows where "id=1", so it must be working if you just specify all your tables with column company_id. You'll need one command to dump all tables with that column, and one command for all other tables (maybe use --ignore-table would come in handy here).

Upvotes: 1

Related Questions