这大致是要走的路:
mysql> SELECT REPLACE('C:\\xampp\\htdocs\\site.com\\foo\\bar.txt', 'C:\\xampp\\htdocs\\site.com', '/var/www/site.com');
+----------------------------------------------------------------------------------------------------------+
| REPLACE('C:\\xampp\\htdocs\\site.com\\foo\\bar.txt', 'C:\\xampp\\htdocs\\site.com', '/var/www/site.com') |
+----------------------------------------------------------------------------------------------------------+
| /var/www/site.com\foo\bar.txt |
+----------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
如果您得到零匹配,那是因为您的数据库记录不包含您认为的内容。确保没有空格或控制字符。如果您的 MySQL 客户端无法轻松发现此类情况,您可以随时使用HEX()
:
SELECT path, HEX(path)
FROM wp_slider
WHERE path NOT LIKE "C:\\xampp\\htdocs\\site.com%"
此外,我不完全确定您可以\
在 Unix 系统中用作路径分隔符。我建议你也更换它:
UPDATE wp_slider
SET path = replace(path, '\\', '/')
WHERE path IS NOT NULL
更新:
我要解释的是您的程序基本上是正确的(除了转义\
并不总是可选的):
mysql> CREATE TABLE wp_slider(
-> path VARCHAR(2083)
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO wp_slider (path) VALUES ('C:\\xampp\\htdocs\\site.com/wp-content/plugins/slider/skins/slider\\circle\\circle.cs
s');
Query OK, 1 row affected (0.04 sec)
mysql> UPDATE wp_slider SET path=REPLACE(path, 'C:\\xampp\\htdocs\\site.com', '/var/www/site.com');
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM wp_slider;
+----------------------------------------------------------------------------+
| path |
+----------------------------------------------------------------------------+
| /var/www/site.com/wp-content/plugins/slider/skins/slider\circle\circle.css |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)
如果您没有得到匹配,那是因为您的数据库包含的数据与您想象的不同,例如(但不限于)空格或控制字符:
mysql> TRUNCATE TABLE wp_slider;
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO wp_slider (path) VALUES ('C:\xampp\htdocs\site.com/wp-content/plugins/slider/skins/slider\circle\circle.css');
Query OK, 1 row affected (0.02 sec)
mysql> UPDATE wp_slider SET path=REPLACE(path, 'C:\\xampp\\htdocs\\site.com', '/var/www/site.com');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> SELECT * FROM wp_slider;
+------------------------------------------------------------------------------+
| path |
+------------------------------------------------------------------------------+
| C:xampphtdocssite.com/wp-content/plugins/slider/skins/slidercirclecircle.css |
+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
In this last example, we forgot to escape \
when inserting and as a result we don't get a match when replacing because the input data is not what we thought it was.