Q:

Mysqldump from a query file

I have a table myTable with columns a, b, c, d. I want to run a query
SELECT a, b FROM myTable where c in (...)

and dump the results. I have this query saved in a file query.sql.
How do I use mysqldump with my file to generate these results? I've only seen examples where you directly write the query in terminal, not using a file.

A:

Mysqldump doesn't run custom queries, it only runs the equivalent of select * from MyTable with optional where clause. So you could do this:
mysqldump --where 'c in (...)' MyDatabase MyTable > dump.sql

But you'd get all the columns of your table, not just a, b.
You can use the mysql client to run custom queries, but it outputs CSV text, not SQL like mysqldump does.
mysql -e 'select a,b from MyTable where c in (...)' MyDatabase > dump.csv

I'm omitting other options like --host, --user, --password because I prefer to keep those in the ~/.my.cnf file.

