MySQL exact full-text matching search method
When doing text searches, you usually want to find exact word matches, not partial matches. For example, a search for "rid" should only return records where "rid" occurs as a complete word, rather than containing words like "arid."
Use REGEXP and word boundary markers in MySQL
In MySQL, you can use the REGEXP operator and the word boundary markers [[:<:]]
and [[:>:]]
to achieve full-text matching. These markers indicate the beginning and end of a word respectively.
<code class="language-sql">SELECT * FROM table WHERE keywords REGEXP '[[:<:]]rid[[:>:]]'</code>
By using these tags, the query can ensure that only rows containing the exact word "rid" are returned.
Updates for MySQL 8.0.4 and above
MySQL 8.0.4 introduces an updated RegExp engine. Therefore, the required word boundary marker has been changed to b
.
<code class="language-sql">SELECT * FROM table WHERE keywords REGEXP '\b rid \b'</code>
Note that the backslash needs to be escaped with a second backslash. By following these steps, you can easily perform full-text matching searches in MySQL, ensuring accurate results.
The above is the detailed content of How to Perform a Whole Word Search in MySQL?. For more information, please follow other related articles on the PHP Chinese website!