2012-11-07 39 views
15

我執行了下面的查詢,並且由於某種原因它沒有替換數據庫中的換行符。它說行數匹配1但沒有變化。什麼可能是錯誤的?替換MYSQL中的換行符不起作用

mysql> UPDATE aboutme SET abouttext=REPLACE(abouttext,'\\n','') WHERE userid='5099a95cd944b8.22468149'; 
Query OK, 0 rows affected (0.00 sec) 
Rows matched: 1 Changed: 0 Warnings: 0 
+0

'abouttext'是你想要存儲替換的列嗎?你確定你不是指'question = REPLACE(問題,'\\ n','')'? –

+0

我認爲查詢發現該行,但它沒有做出更改,因爲'替換'沒有找到'\\ n' – jcho360

回答

24

您可以使用匹配\n,不\\n一個換行符。

代碼:

UPDATE aboutme 
SET abouttext=REPLACE(abouttext,'\n','') 
WHERE userid='5099a95cd944b8.22468149'; 
+0

我甚至用\ n試過並且沒有更新。 –

+1

你可以使用'SELECT'來匹配行嗎,比如'SELECT * FROM aboutme WHERE abouttext LIKE'%\ n%''?您可能需要找到匹配的正確控制字符。見例如本頁列出的代碼:http://dev.mysql.com/doc/refman/5.5/en/regexp.html – Ryan

0

這就是發生

mysql> mysql> select * from t1 limit 3; 
+----------+------------+-----------+---------------------+ 
| actor_id | first_name | last_name | last_update   | 
+----------+------------+-----------+---------------------+ 
|  1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 | 
|  2 | NICK  | WAHLBERG | 2006-02-15 04:34:33 | 
|  3 | ED   | CHASE  | 2006-02-15 04:34:33 | 
+----------+------------+-----------+---------------------+ 
3 rows in set (0.00 sec) 

mysql> update t1 set first_name=replace(first_name,'abc','') where first_name='ed'; 
Query OK, 0 rows affected (0.00 sec) 
Rows matched: 10 Changed: 0 Warnings: 0 

mysql> select * from t1 limit 3; 
+----------+------------+-----------+---------------------+ 
| actor_id | first_name | last_name | last_update   | 
+----------+------------+-----------+---------------------+ 
|  1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 | 
|  2 | NICK  | WAHLBERG | 2006-02-15 04:34:33 | 
|  3 | ED   | CHASE  | 2006-02-15 04:34:33 | 
+----------+------------+-----------+---------------------+ 
3 rows in set (0.00 sec) 


mysql> update t1 set first_name=replace(first_name,'ED','EDD') where first_name='ed'; 
Query OK, 10 rows affected (0.00 sec) 
Rows matched: 10 Changed: 10 Warnings: 0 

mysql> select * from t1 limit 3; 
+----------+------------+-----------+---------------------+ 
| actor_id | first_name | last_name | last_update   | 
+----------+------------+-----------+---------------------+ 
|  1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 | 
|  2 | NICK  | WAHLBERG | 2006-02-15 04:34:33 | 
|  3 | EDD  | CHASE  | 2006-02-15 04:34:33 | 
+----------+------------+-----------+---------------------+ 

3 rows in set (0.00 sec) 

我的意思是,它的工作是,這裏的條件就是爲什麼你有「行匹配:1」,但你更換不找到\\n來取代它,這就是爲什麼changed: 0這樣檢查你的表格數據。

1

您認爲它包含\n,但它具有\r

update [Table] set [column]=replace(convert([column] using utf8) ,'\r',''); 

你的情況:

update aboutme set abouttext=replace(convert(abouttext using utf8) ,'\r',''); 
8

如果\ n沒有在我的情況下工作,下面的工作\ r \ n

UPDATE aboutme 
SET abouttext=REPLACE(abouttext,'\r\n','') 
WHERE userid='5099a95cd944b8.22468149'; 

我的情況是Web應用程序。

0

REPLACE函數是大小寫敏感的,我認爲它屬於MySQL服務器版本

描述= REPLACE(描述中, 'Videosite', 'video.5la.net') 是不同的結果與 描述= REPLACE(描述,'VideoSite','video.5la.net')