Fenomatik
Fenomatik

Reputation: 477

How to run the query from a file in mysql

How can I run a mysql query (just he query part) which is written to a file, eg. accounts.txt

/test/mysql -h testdb.com -P 8995 -p -u testaccount -e 
"select distinct
    amp.account_id
from
    account_marketplace_groups amp;" -D company > /tmp/output.csv

How can I put the actual query in a file and execute the above and still get the output in a csv

Would this be the right way to do it ?

/test/mysql -h testdb.com -P 8995 -p -u testaccount -e /account.txt  -D company > /tmp/output.csv

Upvotes: 1

Views: 395

Answers (2)

Bill Karwin
Bill Karwin

Reputation: 562348

+1 to Barmar's answer but here is another solution, which works equally well:

/test/mysql -h testdb.com -P 8995 -p -u testaccount \
  -e "source /account.txt"  -D company > /tmp/output.csv

That is, if you don't want to use < for input redirection, you can use the -e "source ..." command to read the file from within the command-line.

Upvotes: 1

Barmar
Barmar

Reputation: 781068

The -e option can't be used with a filename, it expects the parameter to be the query. Just use input redirection, since mysql reads from standard input.

/test/mysql -h testdb.com -P 8995 -p -u testaccount  -D company < /account.txt > /tmp/output.csv

Upvotes: 2

Related Questions