使用phpMyAdmin从mySQL数据库中的所有条目中删除紧密匹配的字符串

时间:2016-12-07 14:57:10

标签: mysql wordpress phpmyadmin

我有一个WordPress mysql数据库,我只有phpMyAdmin访问权限。几千个条目(wp_post中的post_content)有一个字符串,类似于我需要删除的post_content中的以下示例(但不是post_content中的任何其他内容)。

如您所见,每个字符串略有不同,但有相似之处。我已经涉及通配符(%和_),但似乎无法通过phpMyAdmin的查找/替换工具得到它。

<!-- ZUJ7vPmaFn --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->
<!-- xqmbbHiYB5 --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->
<!-- Po1o6IUJby --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->
<!-- xqmbbHiYB5 --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->

等...

1 个答案:

答案 0 :(得分:0)

您可以使用此类查询执行此操作。它只测试字符串开头,中间和结尾的修复字符串。它还会检查长度。

SELECT 
  val
, REPLACE(val, SUBSTRING(val,LOCATE('<!-- ',val),60),'') after_replace
FROM testtable
WHERE
    LOCATE('<!-- ',val) > 0
AND
     SUBSTRING(val,15+LOCATE('<!-- ',val),12) = ' --><!-- /* '
AND
     SUBSTRING(val,53+LOCATE('<!-- ',val),7) = ' */ -->'
; 


UPDATE testtable
  set val =  REPLACE(val, SUBSTRING(val,LOCATE('<!-- ',val),60),'') 
WHERE
    LOCATE('<!-- ',val) > 0
AND
     SUBSTRING(val,15+LOCATE('<!-- ',val),12) = ' --><!-- /* '
AND
     SUBSTRING(val,53+LOCATE('<!-- ',val),7) = ' */ -->'
; 

<强>样品

测试数据

mysql> select * from testtable;
+----+-----------------------------------------------------------------------------------------+
| id | val                                                                                     |
+----+-----------------------------------------------------------------------------------------+
|  1 | <!-- ZUJ7vPmaFn --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->                            |
|  2 |      asdfasefasdf<!-- ZUJ7vPmaFn --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->asdfasdfas |
|  3 | 12345<!-- ZUJ7vPmaFn --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->67890                  |
+----+-----------------------------------------------------------------------------------------+
3 rows in set (0,00 sec)

选择示例

mysql> SELECT
    ->   val
    -> , REPLACE(val, SUBSTRING(val,LOCATE('<!-- ',val),60),'') after_replace
    -> FROM testtable
    -> WHERE
    ->     LOCATE('<!-- ',val) > 0
    -> AND
    ->      SUBSTRING(val,15+LOCATE('<!-- ',val),12) = ' --><!-- /* '
    -> AND
    ->      SUBSTRING(val,53+LOCATE('<!-- ',val),7) = ' */ -->'
    -> ;
+-----------------------------------------------------------------------------------------+-----------------------------+
| val                                                                                     | after_replace               |
+-----------------------------------------------------------------------------------------+-----------------------------+
| <!-- ZUJ7vPmaFn --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->                            |                             |
|      asdfasefasdf<!-- ZUJ7vPmaFn --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->asdfasdfas |      asdfasefasdfasdfasdfas |
| 12345<!-- ZUJ7vPmaFn --><!-- /* 1uqjsQSyWVhmOHAEVa1i62html */ -->67890                  | 1234567890                  |
+-----------------------------------------------------------------------------------------+-----------------------------+
3 rows in set (0,00 sec)

mysql>

更新

mysql> UPDATE testtable
    ->   set val =  REPLACE(val, SUBSTRING(val,LOCATE('<!-- ',val),60),'')
    -> WHERE
    ->     LOCATE('<!-- ',val) > 0
    -> AND
    ->      SUBSTRING(val,15+LOCATE('<!-- ',val),12) = ' --><!-- /* '
    -> AND
    ->      SUBSTRING(val,53+LOCATE('<!-- ',val),7) = ' */ -->'
    -> ;
Query OK, 3 rows affected (0,00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from testtable;
+----+-----------------------------+
| id | val                         |
+----+-----------------------------+
|  1 |                             |
|  2 |      asdfasefasdfasdfasdfas |
|  3 | 1234567890                  |
+----+-----------------------------+
3 rows in set (0,00 sec)

mysql>