在phpMyAdmin中使用带有转义字符的REPLACE

时间:2012-01-26 16:23:43

标签: mysql phpmyadmin

我有一个从网站提交的信息数据库。主列数据是使用Markdown输入的,包含许多如下所示的文本:

Walgreens (www.walgreens.com) is the nation\\\'s largest drugstore chain.

我们正在切换到另一个wysiwyg编辑器,需要清理数据。我尝试在phpMyAdmin中执行此操作:

UPDATE sc_answer
SET answer_text = REPLACE (answer_text, '\\\', '')
WHERE sc_answer_id = 24806

但是我收到了错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''\\\', '') WHERE sc_answer_id = 24806' at line 3

任何人都可以帮助我吗?我需要做些什么才能让它发挥作用?

1 个答案:

答案 0 :(得分:4)

对于文本字段中的每个反斜杠\,在SQL中使用2个反斜杠。例如,如果您的表格如下所示:

mysql> select * from foo;
+----+---------------------------------------------------------------------------+
| id | bar                                                                       |
+----+---------------------------------------------------------------------------+
|  1 | Walgreens (www.walgreens.com) is the nation\\\'s largest drugstore chain. |
+----+---------------------------------------------------------------------------+
1 row in set (0.00 sec)

然后

mysql> update foo set bar = REPLACE(bar,'\\\\\\','') where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from foo;
+----+------------------------------------------------------------------------+
| id | bar                                                                    |
+----+------------------------------------------------------------------------+
|  1 | Walgreens (www.walgreens.com) is the nation's largest drugstore chain. |
+----+------------------------------------------------------------------------+
1 row in set (0.00 sec)