如何以CSV格式输出MySQL查询的结果? [英] How to output MySQL query results in CSV format?
本文介绍了如何以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?
下面是我现在在做什么:
Here's what I'm doing now:
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.
推荐答案
从<一个href=\"http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/\">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 '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
使用这个命令列的名字将不会被导出。
Using this command columns names will not be exported.
这篇关于如何以CSV格式输出MySQL查询的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文