如果你有相应的权限,你可以使用 UDF 27.4.2 Adding a New User-Defined Function,一些可以是:
在另一种情况下,如前所述,您可以执行自己的功能,这里是您可以根据需要修改和调整的版本:
mysql> DROP TABLE IF EXISTS `wp_posts`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `wp_posts` (
-> `post_content` TEXT
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `wp_posts`
-> (`post_content`)
-> VALUES
-> ('<img src="http://example.com/img/20150823_image.jpg" style="width: 730px; height: 730px;" />'),
-> ('<img src="http://example.com/img/20150824_image.jpg" style="width: 730px; height: 1500px;" />'),
-> ('<img src="http://example.com/img/20150825_image.jpg" style="width: 730px; height: 80px;" />'),
-> ('<img src="http://example.com/img/20150826_image.jpg" style="width: 730px; height: 0px;" />'),
-> ('<img src="http://example.com/img/20150827_image.jpg" style="width: 730px;" />');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> DELIMITER //
mysql> DROP FUNCTION IF EXISTS `get_string`//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE FUNCTION `get_string`(`_string` TEXT,
-> `_begin` VARCHAR(255),
-> `_end` VARCHAR(255))
-> RETURNS TEXT DETERMINISTIC
-> BEGIN
-> DECLARE `_begin_pos` INT UNSIGNED DEFAULT LOCATE(`_begin`, `_string`);
-> DECLARE `_end_pos` INT UNSIGNED DEFAULT 0;
-> IF `_begin_pos` IS NOT NULL AND `_begin_pos` > 0 THEN
-> SET `_end_pos` := LOCATE(`_end`, `_string`, `_begin_pos`);
-> IF `_end_pos` IS NOT NULL AND `_end_pos` > 0 THEN
-> RETURN SUBSTRING(`_string`,
-> `_begin_pos`,
-> (`_end_pos` + CHAR_LENGTH(`_end`)) - `_begin_pos`);
-> END IF;
-> END IF;
-> RETURN '';
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> SELECT `post_content`
-> FROM `wp_posts`;
+-----------------------------------------------------------------------------------------------+
| post_content |
+-----------------------------------------------------------------------------------------------+
| <img src="http://example.com/img/20150823_image.jpg" style="width: 730px; height: 730px;" /> |
| <img src="http://example.com/img/20150824_image.jpg" style="width: 730px; height: 1500px;" /> |
| <img src="http://example.com/img/20150825_image.jpg" style="width: 730px; height: 80px;" /> |
| <img src="http://example.com/img/20150826_image.jpg" style="width: 730px; height: 0px;" /> |
| <img src="http://example.com/img/20150827_image.jpg" style="width: 730px;" /> |
+-----------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
mysql> UPDATE `wp_posts`
-> SET `post_content` = REPLACE(`post_content`, `get_string`(`post_content`, ' height:', ';'), '');
Query OK, 4 rows affected (0.01 sec)
Rows matched: 5 Changed: 4 Warnings: 0
mysql> SELECT `post_content`
-> FROM `wp_posts`;
+-------------------------------------------------------------------------------+
| post_content |
+-------------------------------------------------------------------------------+
| <img src="http://example.com/img/20150823_image.jpg" style="width: 730px;" /> |
| <img src="http://example.com/img/20150824_image.jpg" style="width: 730px;" /> |
| <img src="http://example.com/img/20150825_image.jpg" style="width: 730px;" /> |
| <img src="http://example.com/img/20150826_image.jpg" style="width: 730px;" /> |
| <img src="http://example.com/img/20150827_image.jpg" style="width: 730px;" /> |
+-------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
Rextester 中的示例。