如何以CSV格式输出MySQL查询结果? [英] How to output MySQL query results in CSV format?

查看:341
本文介绍了如何以CSV格式输出MySQL查询结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以从Linux命令行运行MySQL查询,并在中输出结果CSV 格式?

Is there an easy way to run a MySQL query from the Linux command line and output the results in CSV format?

这里是我现在做的:

mysql -u uid -ppwd -D dbname << EOQ | sed -e 's/        /,/g' | tee list.csv
select id, concat("\"",name,"\"") as name
from students
EOQ

当有很多列需​​要被引号括起来,或者结果中有引号需要转义时, 。

It gets messy when there are a lot of columns that need to be surrounded by quotes, or if there are quotes in the results that need to be escaped.

推荐答案

http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into -a-text-or-csv-file /

SELECT order_id,product_name,qty
FROM orders
WHERE foo = 'bar'
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

使用此命令列名不会导出。

Using this command columns names will not be exported.

这篇关于如何以CSV格式输出MySQL查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆