0

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

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

So I need two mysqldump commands, first will do the where company_id=1 on the tables that I specified and know that they have the column, and the second will be on the rest, and I'll end up with 2 dump files ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.