简体   繁体   中英

MySQL Apostrophe- How to escape and return correct results with MATCH/AGAINST

I have a table sort of like this:

TABLE BOOKS
ID   TITLE             SUBTITLE
------------------------------------
1    Don't Forget      
2    Twenty/Twenty      How To Subtitle
3    Red Riding Hood    Another Subtitle
4    The Three Bears
5    The Threev Bears

MySQL is:

SELECT * FROM books WHERE MATCH (titl, subt) AGAINST ('+don\'t*' IN BOOLEAN MODE)

This Query is returning for me (in phpMyAdmin, I'm still not sure how to escape the apostrophe in PHP) the following:

   ID  TITLE
   -------------------------------
   1   Twenty/Twenty
   2   The Threev Bears

I'm not sure why I'm getting "The Threev Bears" and not "The Three Bears" too? Is "Three" an overlooked word?

Also why am I getting "Twenty/Twenty" but not "Don't Forget" ? That is the only result I want from this.

Thank you for your help!

You've got a few questions in here so let me try to address them.

First, you are correct three is indeed an overlooked word. MySQL has, by default, a list of ignored or "stop" words which aren't indexed. You can adjust this. The idea is to avoid cluttering your index with very common words that will return a large percentage of the results.

To escape characters in PHP you should really be using parameterized queries or at the very least using msql_real_escape_string. Here's a great SO post on the subject

Finally, the reason you're getting Twenty/Twenty , but not Don't forget is because Don't is also a stopword. TwentyTwenty is matching on the t* I believe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM