你可以用這樣的查詢做到這一點。它只在字符串的開始,中間和結尾測試固定字符串。它也檢查長度。
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)
SELECT樣品
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>
請告訴我們什麼ü做了..,我們將幫助您解決.. – m2j