如何使用前缀通配符,例如“*abc”与比赛对决
我有以下查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
匹配不适用于起始通配符,因此与
*abc*
匹配将不起作用。您必须使用LIKE
来实现此目的:但这会非常慢。
如果您确实需要匹配字符串的结尾,并且您必须经常这样做,而性能却让您丧命,解决方案是创建一个单独的列,在其中反转字符串,这样您就得到了
:除了查找
'%abc'
之外,您还可以查找'cba%'
,如果该列已建立索引,速度会快得多。如果您想搜索'cba*'
,您可以再次使用 MATCH。您也只需反转搜索字符串即可。Match doesn't work with starting wildcards, so matching with
*abc*
won't work. You will have to useLIKE
to achieve this: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:
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.我相信选择
全文搜索
与这里无关。如果您有兴趣基于通配符搜索某些字段,例如:%word%
(字符串中任意位置的单词)word%
(以单词开头)%word< /code> (以单词结尾)
最好的选择是使用
LIKE
子句,正如 GolezTrol 提到的那样。但是,如果您对高级/基于文本的搜索感兴趣,可以选择
FULL-TEXT
搜索。LIKE 的限制:
此子句有一些限制。假设您使用类似“%good”的内容(任何以 good 结尾的内容)。它可能会返回不相关的结果,例如
goods
、goody
。因此,请确保您了解自己在做什么以及需要什么。
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.