MySQL 如何从列中删除回车符和换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20577365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:40:43  来源:igfitidea点击:

How to remove carriage returns and line feeds from a column?

mysqlsqlline-breakscarriage-return

提问by Jason Youk

I'm trying to remove carriage returns and line feeds from a column I imported from a csv file.
I'm using the code:

我正在尝试从从 csv 文件导入的列中删除回车符和换行符。
我正在使用代码:

SELECT replace(replace(column,CHAR(13),''),CHAR(10),'')
FROM table

It correctly finds all the CR and LF, but it doesn't update the database.

它正确地找到了所有的 CR 和 LF,但它不会更新数据库。

回答by Joe Taras

Your query retrieve from your table named TABLE all rows with the column replaced.

您的查询从名为 TABLE 的表中检索列被替换的所有行。

About UPDATE your database you must use UPDATE command in this way:

关于 UPDATE 您的数据库,您必须以这种方式使用 UPDATE 命令:

UPDATE table SET column = replace(replace(column,CHAR(13),''),CHAR(10),'')

If you want condition the UPDATE about the satisfaction of some conditions, so you must add the WHERE clause.

如果你想条件 UPDATE 关于满足某些条件,那么你必须添加 WHERE 子句。

For example

例如

UPDATE table SET column = replace(replace(column,CHAR(13),''),CHAR(10),'')
WHERE column_2 = 'XXX'