MYSQL语句中有空格如何解决?

来源:百度知道 编辑:UC知道 时间:2024/06/27 04:34:12
UPDATE cdb_posts SET message=REPLACE(message,'123 456','1234') where tid=123456;

这个替换语句中需要替换的内容有空格,请问该怎么处理呢?
但事实上我执行这个语句了,123 456并没有替换成1234

1、首先replace函数可以做到替换【'123 456'】为【'1234'】,测试log如下:

mysql> select replace('123 456','123 456','1234');
+-------------------------------------+
| replace('123 456','123 456','1234') |
+-------------------------------------+
| 1234 |
+-------------------------------------+
1 row in set (0.05 sec)

2、如果你想去掉【message】中的所有空格的话
【replace(message,' ','')】就可以的。测试log如下:

mysql> select replace('1 2 3 4 5 6',' ','');
+-------------------------------+
| replace('1 2 3 4 5 6',' ','') |
+-------------------------------+
| 123456 |
+-------------------------------+
1 row in set (0.00 sec)

3、所以你的sql改成下面这样就可以了应该:
UPDATE cdb_posts SET message=REPLACE(message,' ','') where tid=123456;