MySQL 搜索单词中带有 $ 的精确单词

发布于 2024-09-29 03:34:28 字数 337 浏览 7 评论 0原文

我一直在试图找到准确的词语,但我的研究对我没有帮助。

我找到的解决方案:

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

但都没有返回我的比赛。

我正在我的数据库中搜索股票代码(例如 $aapl)。如果您搜索 $ba,使用 LIKE '%$word%' 将返回 $bac

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-10-06 03:34:28

您将需要使用 MySql 全文搜索 函数来完成您上面描述的内容。

享受!

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

Enjoy!

虚拟世界 2024-10-06 03:34:28

MATCH AGAINST 的工作方式并不完全符合您的想象。

Casey Fulton 可以将其总结为“...如果结果数量小于总表大小的 50%,全文搜索仅[返回]任何内容...”

因此,我正在搜索所有 LIKE 和然后通过正则表达式过滤掉确切的单词。我这样做是为了减少 REGEX 的进程负载。

以下是我的解决方案:

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

我在 $q 周围放置空格,因为我想匹配句子结构的单词(前面和后面都有一个空格)。

另外,我正在搜索以 $ 开头的“单词”。这是一个问题,这就是我为解决该问题所做的事情:

if(substr($q, 0, 1) == '

祝你好运。

)) AS regexrows "; $query = "SELECT * FROM $regexrows ";

我在 $q 周围放置空格,因为我想匹配句子结构的单词(前面和后面都有一个空格)。

另外,我正在搜索以 $ 开头的“单词”。这是一个问题,这就是我为解决该问题所做的事情:


祝你好运。

) $q = '\\\\' . $q; // you have to escape the backslash that escapes the $ -- it's nuts.

祝你好运。

)) AS regexrows "; $query = "SELECT * FROM $regexrows ";

我在 $q 周围放置空格,因为我想匹配句子结构的单词(前面和后面都有一个空格)。

另外,我正在搜索以 $ 开头的“单词”。这是一个问题,这就是我为解决该问题所做的事情:

祝你好运。

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

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) == '

Best of luck.

)) 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:


Best of luck.

) $q = '\\\\' . $q; // you have to escape the backslash that escapes the $ -- it's nuts.

Best of luck.

)) 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:

Best of luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文