如何将 MySQL 中的几列导出到文本文件 [英] How can I export several columns from MySQL to text file

查看:50
本文介绍了如何将 MySQL 中的几列导出到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将多列导出到一个 csv 文件.使用:

I know how to export multiple columns to a csv file. using:

SELECT `col1`, `col2`
FROM table
INTO OUTFILE 'C:/filename.csv'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';

当我使用excel打开文件时,一切正常,每条记录都在一行中.但是,当我使用记事本打开它时,我发现所有记录都打印在一起,没有行分隔符.如何将查询的输出打印为每行一条记录,每列以逗号分隔??

When I open the file using excel, everything is fine, each record is in a line. But, when I open it using notepad, I find all records are printed next to each other without line separator. How can I make the output of the query to be printed as one record per line and each column separated by comma ??

推荐答案

\n 在linux下是换行符,但是windows使用\r\n

\n is a line break under linux, but windows uses \r\n

相应地,这段代码做了你想要的

Accordingly, this code does what you want

SELECT `col1`, `col2`
FROM table
INTO OUTFILE 'C:/filename.csv'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n';

Excel、Notepad++ 或 WordPad 等许多其他工具都可以使用 \n\r\n 读取文件,因此它们不受此更改的影响并显示使用您的原始代码和此修改正确提交文件.

Many other tools like Excel, Notepad++ or WordPad can read file both with \n and \r\n, so they are not affected by this change and display the file correctly both with your original code and this modification.

这篇关于如何将 MySQL 中的几列导出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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