假设只有空格分隔单词,您可以使用以下内容:-
SELECT SomeId, GROUP_CONCAT(SomeWord ORDER BY aNum SEPARATOR " ")
FROM (SELECT SomeId, SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(" ", SomeText, " "), " ", aNum), " ", -1) AS SomeWord, aNum, item_title_default
FROM SomeTable
CROSS JOIN (SELECT 1 + a.I + b.i * 10 + c.i * 100 AS aNum FROM integers a, integers b, integers c) SubInt
WHERE aNum <= (LENGTH(SomeText) - LENGTH(REPLACE(SomeText, " ", "")) + 2)
AND LENGTH(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(" ", SomeText, " "), " ", aNum), " ", -1)) > 3
ORDER BY SomeId, aNum) Sub1
GROUP BY SomeId
这依赖于一个整数表,其中有一列称为 i,行的值为 0 到 9。它最多可处理 1000 个单词,但很容易扩展。
编辑 - 好的,这是一个 MySQL 存储过程,用于拆分字符串并删除长度为 3 个或更少字符的任何单词。同样只在空格上分裂。
DELIMITER //
CREATE FUNCTION `func_Remove_ShortStrings`(InStr LONGTEXT) RETURNS LONGTEXT
DETERMINISTIC
BEGIN
DECLARE RetStr LONGTEXT DEFAULT "";
DECLARE TempStr LONGTEXT DEFAULT "";
DECLARE aWord LONGTEXT DEFAULT "";
DECLARE WordLength INT DEFAULT 0;
SET TempStr = TRIM(InStr);
WHILE (LENGTH(TempStr) > 0) DO
SET aWord = SUBSTRING_INDEX(TempStr, " ", 1);
SET WordLength = LENGTH(aWord);
IF (WordLength > 3) THEN
SET RetStr = CONCAT(RetStr, " ", aWord);
END IF;
SET TempStr = TRIM(SUBSTRING(TempStr, WordLength + 1));
END WHILE;
return TRIM(RetStr);
END