搜索电子邮件地址时 MySQL MATCH AGAINST

发布于 2024-12-28 11:17:14 字数 1033 浏览 3 评论 0原文

我正在编写新闻通讯脚本,我需要在地址中实现搜索。我使用 FULLTEXT 对表建立了索引,但是当我执行以下查询时:

SELECT * FROM addresses WHERE MATCH(email) AGAINST("[email protected]" IN BOOLEAN MODE)

我得到了奇怪的结果。它显示“example.com”上的所有电子邮件以及用户“name”的所有电子邮件。例如,我得到:

[email protected]
[email protected]
[email protected]

我重写了查询以使用 LIKE "%[email protected]%”,但是对于一个大表来说,完成它需要大量的时间。 有解决办法吗?我希望在搜索时仅显示完整匹配的电子邮件而不是其中的一部分。先感谢您。

I am writing a newsletter script and I need to implement searching in the addresses. I indexed the table with FULLTEXT but when I do a query such as:

SELECT * FROM addresses WHERE MATCH(email) AGAINST("[email protected]" IN BOOLEAN MODE)

I get strange results. It displays all emails on "example.com" and all emails with user "name". For example I get:

[email protected]
[email protected]
[email protected]

I rewrote the query to use LIKE "%[email protected]%" but for a big table it takes ridiculous amount of time to complete.
Is there a solution for this? I want when searching to show only full matching emails and not part of them. Thank you in advance.

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

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

发布评论

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

评论(2

淡忘如思 2025-01-04 11:17:14

要匹配精确的短语,您必须将该短语括在双引号中。因此,将查询更改为:

SELECT * FROM 地址 WHERE MATCH(email) AGAINST('"[电子邮件受保护]“”布尔模式)

来源

To match an exact phrase you have to enclose the phrase in double quotes. So change your query to:

SELECT * FROM addresses WHERE MATCH(email) AGAINST('"[email protected]"' IN BOOLEAN MODE)

Source

少女情怀诗 2025-01-04 11:17:14

您可以使用传统索引(带有简单的 = 运算符)而不是全文索引。它将运行速度快并且在数据库引擎上使用更少的资源。

SELECT * FROM addresses WHERE email = '[email protected]';

在这种情况下,如果您想通过电子邮件地址的开头部分进行查找,也可以使用类似运算符,在搜索字符串末尾添加 %。

SELECT * FROM addresses WHERE email like 'name@exam%';
 

You can use a traditional index (with a simple = operator) instead of a fulltext index. It will work fast and use less resource on DB engine.

SELECT * FROM addresses WHERE email = '[email protected]';

In this case, it is also possible to use a like operator, with % at the end of search string if you want to find by initial part of email address.

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