How to use prefix wildcards like '*abc' with match-against

房东的猫 提交于 2019-12-31 01:49:11

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7848445/how-to-use-prefix-wildcards-like-abc-with-match-against

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!