在mysql 里 提取某个带有html标签的内容(content字段)插入到简介里(description字段)
首先创建mysql去除html函数
DELIMITER //
CREATE FUNCTION `strip_tags`($str text) RETURNS text
BEGIN
DECLARE $start, $end INT DEFAULT 1;
LOOP
SET $start = LOCATE("<", $str, $start);
IF (!$start) THEN RETURN $str; END IF;
SET $end = LOCATE(">", $str, $start);
IF (!$end) THEN SET $end = $start; END IF;
SET $str = INSERT($str, $start, $end - $start + 1, "");
END LOOP;
END;
//
DELIMITER ;再使用mysql内置截取函数left(), right(), substring(), substring_index()。还有 mid(), substr() 来截取部分内容插入到简介字段中(截取函数使用详细介绍)
最后的sql语句为:
UPDATE `xt_news` SET `description`=(select left(strip_tags(content),500) from `xt_news_data` where xt_news.id=xt_news_data.id) WHERE 1
1253

被折叠的 条评论
为什么被折叠?



