我已经复制了一个sql函数,以尝试在搜索操作期间使用它过滤掉HTML数据,但是使用该函数会在标题中引发错误。
如果需要,这是函数+ drop语句:
DROP FUNCTION IF EXISTS StripTags;
DELIMITER |
CREATE FUNCTION StripTags( Dirty varchar(255) )
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE iStart, iEnd, iLength int;
WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Dirty, Locate( '<', Dirty )) > 0 DO
BEGIN
SET iStart = Locate( '<', Dirty ), iEnd = Locate( '>', Dirty, Locate('<', Dirty ));
SET iLength = ( iEnd - iStart) + 1;
IF iLength > 0 THEN
BEGIN
SET Dirty = Insert( Dirty, iStart, iLength, '');
END;
END IF;
END;
END WHILE;
RETURN Dirty;
END;
|
DELIMITER ;
这是查询。如您所见,我正在搜索'p'以检查结果中是否存在
标记。
SELECT
*
FROM (
SELECT
`products`.*,
max(( CASE WHEN LOWER(StripTags(`product_code`))
LIKE 'p' THEN
600
ELSE
0
END) + ( CASE WHEN LOWER(StripTags(`product_code`))
LIKE 'p%' THEN
200
ELSE
0
END) + ( CASE WHEN LOWER(StripTags(`product_code`))
LIKE '%p%' THEN
40
ELSE
0
END) + ( CASE WHEN LOWER(StripTags(`description`))
LIKE 'p' THEN
600
ELSE
0
END) + ( CASE WHEN LOWER(StripTags(`description`))
LIKE 'p%' THEN
200
ELSE
0
END) + ( CASE WHEN LOWER(StripTags(`description`))
LIKE '%p%' THEN
40
ELSE
0
END)) AS relevance
FROM
`products`
WHERE
`products`.`organisation_id` = '2'
GROUP BY
`products`.`id`
HAVING
relevance >= 40.00
ORDER BY
`relevance` DESC) AS `products`
WHERE
`products`.`organisation_id` = '2'
ORDER BY
`products`.`created_at` DESC
LIMIT 200 offset 0;
我尝试在不同的位置使用不同的排序规则,但是除了一些不同的错误(COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'utf8mb4'
)之外,我觉得自己离答案更近了。
如果有人有任何想法,请告诉我!。