2018-4-28 MySQL remove line feeds and carriage returns in fields

2018-4-28 MySQL remove line feeds and carriage returns in fields

question

There will be multiple line breaks in the csv exported by mysqld because there are line breaks and carriage returns in the fields.

Solution

UPDATE finall_2 SET loan_time = REPLACE(REPLACE(loan_time, CHAR(10),''), CHAR(13),'');

char(10): newline
char(13): carriage return

The cause of the problem:

There are 2 methods to generate reports in excel mode:

  • Manual Generation
    Export the data in the table to generate a CSV file.
    Use mysqldump to export data
    #mysqldump -u xxx -p --tab=/tmp/ --fields-terminated-by="#" DBName TBName
    will generate a TBName.txt file in the tmp directory.
    Import the generated txt file in EXCEL
  • 直接生成csv格式文件
    mysqldump -u samu -p -T --fields-terminated-by="," --fields-enclosed-by=""
    --lines-terminated-by="\n" --fields-escaped-by="" test Customer
    或者:
    mysqldump -u samu -p --tab=/tmp/ --fields-terminated-by="," --fields-enclosed-by=""
    --lines-terminated-by="\n" --fields-escaped-by="" test Customer

但是,无论上面哪一种方法,如果表的某个列里包含回车符或者换行符,那么生成的CSV文件或者进行excel导入,都会将原本的1行数据,拆分成2行。因为CSV或者excel导入,是按数据的行来认定数据条数。

所以,必须在此之前,将字段中的回车符或者换行符,进行替换。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324999702&siteId=291194637