繁体   English   中英

MySQL查询以查找并替换指向目录根目录但不指向目录下子目录的URL

[英]MySQL query to find and replace urls that point to the root of a directory but not a subdirectory underneath it

我在WordPress核心升级中遇到问题,它删除了我的上载目录根目录中的图像。

为了解决此问题,我尝试通过WordPress重新上传图像,并按年份和月份逐月将帖子内容中的URL指向子目录中其新位置。

因此,我需要对MySQL查询做的基本上是:

查找:位于“ wp-content / uploads”根目录中的文件, 但不位于“ wp-content / uploads / 2014 / ...”之类的其他目录下

对于这些结果,将“ wp-content / uploads”替换为:“ wp-content / uploads / 2015/04”。

我可能会使用类似这样的查询:

UPDATE wp_posts SET post_content = replace(post_content, 'oldpath', 'newpath');

有没有一种方法只能找到与“ uploads”目录的根目录匹配的结果,而不要查找另一个目录?

您可以使用regexp查找匹配或不匹配的行,并根据该条件进行更新。

update wp_posts
  set post_content = replace(post_content, 'oldpath', 'newpath')
  where post_content not regexp 'wp-content/uploads/.*/.*'

这是一个简单的正则表达式,但是应该可以完成工作。

mysql> select 'wp-content/uploads/1.jpg' not regexp 'wp-content/uploads/.*/.*';
+------------------------------------------------------------------+
| 'wp-content/uploads/1.jpg' not regexp 'wp-content/uploads/.*/.*' |
+------------------------------------------------------------------+
|                                                                1 |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select 'wp-content/uploads/2014/1.jpg' not regexp 'wp-content/uploads/.*/.*';
+-----------------------------------------------------------------------+
| 'wp-content/uploads/2014/1.jpg' not regexp 'wp-content/uploads/.*/.*' |
+-----------------------------------------------------------------------+
|                                                                     0 |
+-----------------------------------------------------------------------+
1 row in set (0.00 sec)

更新了正则表达式,使其更具排他性
更改'wp-content/uploads/.*/.*'; 'wp-content/uploads/[^/]*/[^"]*"'; 它将在您在下面发布的小提琴中起作用,请在此处查看更新的版本: http//sqlfiddle.com/#!9 / d636b / 1 (请当然在执行此操作之前先进行备份)

暂无
暂无

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

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