我正在尝试清理超过165个字符的太长的元描述。我有这个问题:
UPDATE ps_product_lang
SET meta_description = REPLACE(meta_description, 'Depth 12 mm', '')
唯一的麻烦是有各种各样的深度,而不是用一个替换它们,有没有办法为它们所有人做?像
这样的东西UPDATE ps_product_lang
SET meta_description = REPLACE(meta_description, 'Depth * mm', '') * WHERE = any value
答案 0 :(得分:0)
MySQL没有一个简单的解决方案,因为REPLACE()不支持模式,通配符或正则表达式。
我建议使用这样的东西:
/* EXTENDED Functionality allows user to use patterns up to 100 characters long, not just 1 character
/* Original @ http://www.nacodes.com/#ECHo75Y7RYXbgow0.99 */
DELIMITER $$
CREATE FUNCTION `regex_replace`(pattern VARCHAR(100),replacement VARCHAR(1000),original VARCHAR(1000))
RETURNS VARCHAR(1000)
DETERMINISTIC
BEGIN
DECLARE temp VARCHAR(1000);
DECLARE ch VARCHAR(100);
DECLARE i INT;
DECLARE j INT;
DECLARE qbTemp VARCHAR(1000);
SET i = 1;
SET j = 1;
SET temp = '';
SET qbTemp = '';
IF original REGEXP pattern THEN
loop_label: LOOP
IF i>CHAR_LENGTH(original) THEN
LEAVE loop_label;
END IF;
SET ch = SUBSTRING(original,i,CHAR_LENGTH(pattern));
IF NOT ch REGEXP pattern THEN
SET temp = CONCAT(temp,LEFT(ch,1));
SET i=i+1;
ELSE
SET temp = CONCAT(temp,replacement);
SET i=i+CHAR_LENGTH(pattern);
END IF;
END LOOP;
ELSE
SET temp = original;
END IF;
SET temp = TRIM(BOTH replacement FROM temp);
SET temp = REPLACE(REPLACE(REPLACE(temp , CONCAT(replacement,replacement),CONCAT(replacement,'#')),CONCAT('#',replacement),''),'#','');
RETURN temp;
END$$
DELIMITER ;
作为替代方案,您始终可以将数据导出到文件中,例如,在文本编辑器或sed中更改字段,然后将其导回MySQL。