MySQL search exact word with $ in word

左心房为你撑大大i 提交于 2019-12-02 21:46:27

问题


I've been trying to find exact words and my research isn't helping me.

Solutions I've found:

$query .= "WHERE text REGEXP '[[:<:]]($word)[[:>:]]'";
// OR
$query .= "WHERE MATCH(text) AGAINST('$word') ";

but neither are returning my matches.

I'm searching for stock symbols in my db (eg $aapl). And using LIKE '%$word%' will return $bac if you search for $ba.


回答1:


You will want to use MySql Full Text Search functions to accomplish what you describe above.

Enjoy!




回答2:


MATCH AGAINST doesn't work exactly how you'd think it would.

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..."

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.

The following is my solution:

$likerows = "(SELECT * FROM `tweets` WHERE text LIKE '%$q%') AS likerows ";
$regexrows = "(SELECT * FROM $likerows WHERE text REGEXP('^.* $q .*$')) AS regexrows ";
$query = "SELECT * FROM $regexrows ";

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.

Best of luck.



来源:https://stackoverflow.com/questions/3999180/mysql-search-exact-word-with-in-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!