开发者

Using MySQL in Powershell, how do I pipe the results of my script into a csv file?

开发者 https://www.devze.com 2023-02-07 03:07 出处:网络
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 开发者_StackOverflowa small set of columns that I would like copied in

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 开发者_StackOverflowa 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!


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)


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号