1、使用mysql的REGEXP正则匹配出相关内容,再使用LOCATE('{$keywordOld}',{$keywordCode}) DESC 统计出关键词匹配次数进行排序排序,
// LOCATE('$keywordOld',$keywordCode) DESC 排序使用
$keywordOld = addcslashes($keyword, "'");
$keywordArr = explode(' ', $keywordOld);
//关键词转义 ' " * . ? + $ ^ [ ] ( ) { } | \ / ! - : =
//转义特殊字符 \* \. \?
$keyword = addcslashes(mb_strtolower($keyword, 'utf-8'), '*.?+$^[](){}|\\/!-:=%@');
$keyword = preg_replace("/\s+/", "|", $keyword); //把空格替换成 |
$keywordNew = $keyword;
$keyword = addcslashes($keyword, "'");
//mysql正则匹配必须 把 \+ ,替换成\\+
$keywordNew = addcslashes($keywordNew, '\\');
//特殊字符 MySQL 语句安全转义 ' "
$keywordNew = addcslashes($keywordNew, "'");
$keywordNew = " REGEXP '{$keywordNew}'";
$keywordCode = "LCASE(CONCAT(name,' ',user_name))";
$code = "id, user_name, FROM_UNIXTIME(insert_time) AS insert_time";
$where = "WHERE is_show = 1 AND {$keywordCode} REGEXP '{$keywordNew}' ";
$codeLen = strlen ($keywordOld); //关键词长度
$mysqlLen = "LENGTH($keywordCode)";
$by = "($mysqlLen - LENGTH(REPLACE($keywordCode,'$keywordOld','')))/$codeLen DESC, "; //返回计算关键词匹配次数
if($kCount>1) {
foreach ($keywordArr as $m => $item) {
$codeLen = strlen ($item);
$by .= "($mysqlLen - LENGTH(REPLACE($keywordCode,'$item','')))/$codeLen";
if($m != $kCount -1){
$by .= " + ";
}
}
$by .= " DESC, ";
}
$by .= "insert_time DESC";
$sql = "SELECT {$code} FROM category {$where} UNION ALL SELECT {$code} FROM files {$where} {$by} limit 500";
//替换关键词
$name = preg_replace("/($keyword)/i", "\\1", $name);
$userName = preg_replace("/($keyword)/i", "\\1", $userName);
2、最终的SQL语句
SELECT
id,
user_name,
CONCAT(NAME, ' ', user_name) AS basename,
FROM_UNIXTIME(insert_time) AS insert_time
FROM
category
WHERE is_show = 1
AND basename REGEXP 'a|b|c'
UNION
ALL
SELECT
id,
CONCAT(NAME, ' ', user_name) AS basename,
FROM_UNIXTIME(insert_time) AS insert_time
FROM
files
WHERE is_show = 1
AND basename REGEXP 'a|b|c'
ORDER BY (
LENGTH(basename) - LENGTH(REPLACE(basename, 'a b c', ''))
) / 5 DESC,
(
LENGTH(basename) - LENGTH(REPLACE(basename, 'a', ''))
) / 1 + (
LENGTH(basename) - LENGTH(REPLACE(basename, 'b', ''))
) / 1 + (
LENGTH(basename) - LENGTH(REPLACE(basename, 'c', ''))
) / 1 DESC,
insert_time DESC
LIMIT 300
再总结一下常用的正在表达式。