搜索电子邮件地址时 MySQL MATCH AGAINST
我正在编写新闻通讯脚本,我需要在地址中实现搜索。我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要匹配精确的短语,您必须将该短语括在双引号中。因此,将查询更改为:
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
您可以使用传统索引(带有简单的 = 运算符)而不是全文索引。它将运行速度快并且在数据库引擎上使用更少的资源。
在这种情况下,如果您想通过电子邮件地址的开头部分进行查找,也可以使用类似运算符,在搜索字符串末尾添加 %。
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.
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.