简体   繁体   English

使用SELECT mysql查询将数据导出到CSV文件

[英]exporting data to CSV file using SELECT mysql query

Usually, to export CSV reports from a mysql server, I connect & run a query on the server, save the results into a text file, and import the file to excel by specifying to separate the columns using the pipe delimited character. 通常,要从mysql服务器导出CSV报告,我会在服务器上连接并运行查询,将结果保存到文本文件中,然后通过指定使用竖线分隔字符分隔列将文件导入excel。

I need to run a report on a table with a MEDIUMTEEXT column type, which contains comma characters, and line breaks (\\n). 我需要在具有MEDIUMTEEXT列类型的表上运行报告,该表类型包含逗号和换行符(\\ n)。

The line breaks and commas symbols are breaking the table layout. 换行符和逗号符号破坏了表格的布局。

SELECT `number`,REPLACE(`description`, ',', ''),  mr.`dateInserted` FROM 
`mr`  WHERE mr.dateInserted >= '2012-01-01' AND mr.dateInserted <= '2012-01-31'

I solved the comma issues by using the MySQL REPLACE function, however, how can I remove the line breaks from the results? 我使用MySQL REPLACE函数解决了逗号问题,但是,如何从结果中删除换行符呢?

You can do this with an query, here's an example. 您可以通过查询来执行此操作,这是一个示例。

SELECT
  id,
  name,
  email
INTO
  OUTFILE '/tmp/result.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY ‘\\’
LINES TERMINATED BY '\n'
FROM users WHERE 1

You can also use a combination of a "view" and "mysqldump". 您也可以结合使用“视图”和“ mysqldump”。

http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html

Char(13) is a carriage return Char(13)是回车符

Char(10) is a line feed Char(10)是换行符

Try nested replace functions for path of least resistance :) 尝试嵌套的替换函数以获取阻力最小的路径:)

SELECT 
    `number`, 
    REPLACE(REPLACE(REPLACE(`description`, ',', ''), CHAR(13),''), CHAR(10),''),
    mr.`dateInserted`
FROM
    `mr`
WHERE
    mr.dateInserted >= '2012-01-01' 
AND 
    mr.dateInserted <= '2012-01-31'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM