我试图从句子(字符串)中获得精确的单词(子字符串)匹配,其中包含至少一个确切的单词匹配(如果不是文章或动名词,最好,如果不是文章或动名词),存储在句子中任何位置的数据库中,如下所示: / p>
SELECT q
FROM q
WHERE q LIKE '%$q%' OR '$q' LIKE CONCAT('%', q, '%')
ORDER BY CASE WHEN q = '$q' THEN 0
WHEN q LIKE '$q%' THEN 1
WHEN q LIKE '%$q%' THEN 2
WHEN q LIKE '%$q' THEN 3
ELSE 4
END, q ASC
LIMIT 10;
但它返回值为$q = "best seo company for small business";
的查询的以下结果:
best seo company for small business
best seo
best seo company
mpa
seo
seo com
seo company
small
small b
small bu
期望的产出:
best seo company for small business
best seo
best seo company
seo
seo company
small
不受欢迎的产出:
mpa
seo com
small b
small bu
如何排除不需要的输出?
注意:以下问题:
Search for "whole word match" in MySQL
Make MySQL LIKE Return Full Word matches Only
MySQL Finding Substring Matches and Group by Match Full Word
建议一些与我正在寻找的解决方案无关的答案。
答案 0 :(得分:1)
解决方法可以是 REGEXP q+
It matches any string containing at least one q
。
虽然它会与seo company
匹配:
affordable seo company
atlanta seo company
austin seo company
automotive seo company
best atlanta seo company
best local seo company
best los angeles seo company
best organic seo company
best real estate seo company
best seo company
best seo company for google
best seo company for lawyers
best seo company for small business
best seo company in india
best seo company in the usa
best seo company in the world
best seo company india
best seo company los angeles
best seo company miami
boise seo company
boston seo company
california seo company
charleston seo company
charlotte seo company
cheap seo company
它不能将best seo company for small business
与上述任何条款匹配,除了它本身。
试试这个:
SELECT q
FROM q
WHERE q REGEXP '$q+'
ORDER BY CASE WHEN q = '$q' THEN 0
WHEN q LIKE '$q%' THEN 1
WHEN q LIKE '%$q%' THEN 2
WHEN q LIKE '%$q' THEN 3
ELSE 4
END, q ASC
LIMIT 10
答案 1 :(得分:0)
WHERE '$q' REGEXP '[[:<:]]q[[:>:]]'
但是,要小心。如果$q
中有任何标点符号,则可能会出现语法错误。所以,一定要逃避它。