Andrew
Andrew

Reputation: 6860

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.

Upvotes: 2

Views: 2158

Answers (3)

Salehin Rafi
Salehin Rafi

Reputation: 351

You can use mysql cli...

mysql -e "SELECT a, b FROM myTable where c in (...)" -h[hostname] -u[username] -p[password] [dbname]

If you want to redirect it out to a file if you want:

mysql -e "SELECT a, b FROM myTable where c in (...)" -h[hostname] -u[username] -p[password] [dbname] > mydumpfile.txt

Upvotes: 0

Bill Karwin
Bill Karwin

Reputation: 562260

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.

Upvotes: 1

FreedomPride
FreedomPride

Reputation: 1102

You can use this function INTO OUTFILE

SELECT a, b FROM myTable where c in (...)
INTO OUTFILE '/tmp/orders.txt'

Upvotes: 0

Related Questions