MySQL用单词$搜索确切的单词 [英] MySQL search exact word with $ in word
问题描述
我一直在寻找确切的单词,但是我的研究没有帮助我.
I've been trying to find exact words and my research isn't helping me.
我发现的解决方案:
$query .= "WHERE text REGEXP '[[:<:]]($word)[[:>:]]'";
// OR
$query .= "WHERE MATCH(text) AGAINST('$word') ";
但都没有返回我的比赛.
but neither are returning my matches.
我正在数据库中搜索股票代码(例如$aapl
).如果您搜索$ba
,则使用LIKE '%$word%'
将返回$bac
.
I'm searching for stock symbols in my db (eg $aapl
). And using LIKE '%$word%'
will return $bac
if you search for $ba
.
推荐答案
再次匹配并不能完全按照您的想法工作.
MATCH AGAINST doesn't work exactly how you'd think it would.
Casey Fulton可以将其概括为"...如果结果数少于表总大小的50%,... FULLTEXT仅搜索[返回]任何内容."
This can be summarized by Casey Fulton as "...FULLTEXT searches only [return] anything if the number of results is less than 50% of the total table size..."
因此,我正在搜索所有LIKE,然后通过REGEX过滤掉确切的单词.我这样做是为了减少REGEX的处理负担.
So instead, I'm searching for all the LIKEs and then filtering out exact words by a REGEX. I'm doing this to cut down on the process load for REGEX.
以下是我的解决方法:
$likerows = "(SELECT * FROM `tweets` WHERE text LIKE '%$q%') AS likerows ";
$regexrows = "(SELECT * FROM $likerows WHERE text REGEXP('^.* $q .*$')) AS regexrows ";
$query = "SELECT * FROM $regexrows ";
我想在$ q周围加上空格,因为我想匹配一个句子结构的单词(该单词的前面是一个空格).
I put spaces around the $q because I want to match a sentence-structured word (which is preceeded and followed by a space).
此外,我正在搜索以$开头的单词".这是一个问题,这是我为解决该问题所做的事情:
Also, I'm searching for "words" that start with $. This was an issue and here's what I did to solve that problem:
if(substr($q, 0, 1) == '$') $q = '\\\\' . $q; // you have to escape the backslash that escapes the $ -- it's nuts.
祝你好运.
这篇关于MySQL用单词$搜索确切的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!