如何使用前缀通配符,例如“*abc”与比赛对决

发布于 2024-12-11 13:53:55 字数 363 浏览 0 评论 0原文

我有以下查询:

SELECT * FROM `user` 
WHERE MATCH (user_login) AGAINST ('supriya*' IN BOOLEAN MODE)

输出以 'supriya' 开头的所有记录。
现在我想要一些可以找到所有以 'abc' 结尾的记录的东西。
我知道 * 不能被预先附加,它也不起作用,我已经搜索了很多,但找不到任何与此相关的内容。

如果我给查询字符串 priya ..它应该返回所有以 priya 结尾的记录。
我该怎么做?

I have the following query :

SELECT * FROM `user` 
WHERE MATCH (user_login) AGAINST ('supriya*' IN BOOLEAN MODE)

Which outputs all the records starting with 'supriya'.
Now I want something that will find all the records ending with e.g. 'abc'.
I know that * cannot be preappended and it doesn't work either and I have searched a lot but couldn't find anything regarding this.

If I give query the string priya ..it should return all records ending with priya.
How do I do this?

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

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

发布评论

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

评论(2

ゝ杯具 2024-12-18 13:53:55

匹配不适用于起始通配符,因此与 *abc* 匹配将不起作用。您必须使用 LIKE 来实现此目的:

SELECT * FROM user WHERE user_login LIKE '%abc';

但这会非常慢。

如果您确实需要匹配字符串的结尾,并且您必须经常这样做,而性能却让您丧命,解决方案是创建一个单独的列,在其中反转字符串,这样您就得到了

user_login user_login_rev
xyzabc     cbazyx

:除了查找 '%abc' 之外,您还可以查找 'cba%',如果该列已建立索引,速度会快得多。如果您想搜索 'cba*',您可以再次使用 MATCH。您也只需反转搜索字符串即可。

Match doesn't work with starting wildcards, so matching with *abc* won't work. You will have to use LIKE to achieve this:

SELECT * FROM user WHERE user_login LIKE '%abc';

This will be very slow however.

If you really need to match for the ending of the string, and you have to do this often while the performance is killing you, a solution would be to create a separate column in which you reverse the strings, so you got:

user_login user_login_rev
xyzabc     cbazyx

Then, instead of looking for '%abc', you can look for 'cba%' which is much faster if the column is indexed. And you can again use MATCH if you like to search for 'cba*'. You will just have to reverse the search string as well.

裸钻 2024-12-18 13:53:55

我相信选择全文搜索与这里无关。如果您有兴趣基于通配符搜索某些字段,例如:

  • %word%(字符串中任意位置的单词)
  • word%(以单词开头)
  • %word< /code> (以单词结尾)

最好的选择是使用 LIKE 子句,正如 GolezTrol 提到的那样。

但是,如果您对高级/基于文本的搜索感兴趣,可以选择FULL-TEXT 搜索。

LIKE 的限制

此子句有一些限制。假设您使用类似“%good”的内容(任何以 good 结尾的内容)。它可能会返回不相关的结果,例如 goodsgoody

因此,请确保您了解自己在做什么以及需要什么。

I believe the selection of FULL-TEXT Searching isn't relevant here. If you are interested in searching some fields based on wildcards like:

  • %word% ( word anywhere in the string)
  • word% ( starting with word)
  • %word ( ending with word)

best option is to use LIKE clause as GolezTrol has mentioned.

However, if you are interested in advanced/text based searching, FULL-TEXT search is the option.

Limitations with LIKE:

There are some limitations with this clause. Let suppose you use something like '%good' (anything ending with good). It may return irrelevant results like goods, goody.

So make sure you understand what you are doing and what is required.

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