简体   繁体   English

如何从mysql中的数据行中删除换行符?

[英]How to remove new line characters from data rows in mysql?

I can loop through all of the rows in a php script and do我可以遍历 php 脚本中的所有行并执行

UPDATE mytable SET title = "'.trim($row['title']).'" where id = "'.$row['id'].'";

and trim can remove \\n和修剪可以删除\\n

But I was just wondering if something same could be done in one query?但我只是想知道是否可以在一个查询中完成相同的事情?

 update mytable SET title = TRIM(title, '\n') where 1=1

will it work?它会起作用吗? I can then just execute this query without requiring to loop through!然后我可以执行这个查询而不需要循环!

thanks谢谢

(PS: I could test it but table is quite large and dont want to mess with data, so just thought if you have tested something like this before) (PS:我可以测试,但表很大,不想弄乱数据,所以想想你之前是否测试过这样的东西)

UPDATE test SET log = REPLACE(REPLACE(log, '\r', ''), '\n', '');

worked for me.为我工作。

while its similar, it'll also get rid of \\r\\n虽然它相似,但它也会摆脱\\r\\n

http://lists.mysql.com/mysql/182689 http://lists.mysql.com/mysql/182689

your syntax is wrong:你的语法错误:

update mytable SET title = TRIM(TRAILING '\n' FROM title)

Addition:添加:

If the newline character is at the start of the field:如果换行符位于字段的开头:

update mytable SET title = TRIM(LEADING '\n' FROM title)

1) Replace all new line and tab characters with spaces. 1) 用空格替换所有新行制表符。

2) Remove all leading and trailing spaces. 2) 删除所有前导和尾随空格。

 UPDATE mytable SET `title` = TRIM(REPLACE(REPLACE(REPLACE(`title`, '\n', ' '), '\r', ' '), '\t', ' '));

update mytable set title=trim(replace(REPLACE(title,CHAR(13),''),CHAR(10),''));

以上工作正常。

Removes trailing returns when importing from Excel.从 Excel 导入时删除尾随返回。 When you execute this, you may receive an error that there is no WHERE;当您执行此操作时,您可能会收到没有 WHERE 的错误; ignore and execute.忽略并执行。

UPDATE table_name SET col_name = TRIM(TRAILING '\r' FROM col_name)
UPDATE mytable SET title=TRIM(REPLACE(REPLACE(title, "\n", ""), "\t", ""));

玩上面的答案,这个对我有用

REPLACE(REPLACE(column_name , '\n', ''), '\r', '')

My 2 cents.我的 2 美分。

To get rid of my \\n's I needed to do a \\\\n.为了摆脱我的\\n,我需要做一个\\\\n。 Hope that helps someone.希望能帮助某人。

update mytable SET title = TRIM(TRAILING '\\n' FROM title)

For new line characters对于换行符

UPDATE table_name SET field_name = TRIM(TRAILING '\n' FROM field_name);
UPDATE table_name SET field_name = TRIM(TRAILING '\r' FROM field_name);
UPDATE table_name SET field_name = TRIM(TRAILING '\r\n' FROM field_name);

For all white space characters对于所有空白字符

UPDATE table_name SET field_name = TRIM(field_name);
UPDATE table_name SET field_name = TRIM(TRAILING '\n' FROM field_name);
UPDATE table_name SET field_name = TRIM(TRAILING '\r' FROM field_name);
UPDATE table_name SET field_name = TRIM(TRAILING '\r\n' FROM field_name);
UPDATE table_name SET field_name = TRIM(TRAILING '\t' FROM field_name);

Read more: MySQL TRIM Function阅读更多: MySQL TRIM 函数

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

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