0

In PowerShell, how do I execute my mysql script so that the results are piped into a csv file? The results of this script is just a small set of columns that I would like copied into a csv file.

I can have it go directly to the shell by doing:

mysql> source myscript.sql

And I have tried various little things like:

mysql> source myscript.sql > mysql.out
mysql> source myscript.sql > mysql.csv

in infinite variation, and I just get errors. My db connections is alright because I can do basic table queries from the command line etc... I haven't been able to find a solution on the web so far either...

Any help would be really appreciated!

1

2 Answers 2

1

You seem to not be running powershell, but the mysql command line tool (perhaps you started it in a powershell console though.)

Note also that the mysql command line tool cannot export directly to csv.

However, to redirect the output to a file just run

mysql mydb < myscript.sql >mysql.out

or e.g.

echo select * from mytable |  mysql mydb >mysql.out

(and whatever arguments to mysql you need, like username, hostname)

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

2 Comments

For the first one, I tried it in both shell and mysql commandline (running in shell). I either get "'>' character reserved for future use" or I am told I have a syntax error (when I try running within mysql terminal). Pretty new at this :) Thanks for your help!
@yunje you should not run this in the mysql terminal. Run it in your cmd.exe or powershell
0

Are you looking for SELECT INTO OUTFILE ? dev.mysql.com/doc/refman/5.1/en/select.html – Pekka 19 hours ago

Yep. Select into outfile worked! But to make sure you get column names you also need to do something like:

select *
from
(
 select 
 a,
 b,
 c
 )
 Union ALL
(Select *
from actual)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.