Performing a "whole word match" search in MySQL
When searching for keywords in a MySQL database, you usually need to match the entire word exactly, rather than a partial match. This is especially useful when searching for terms like "rid" as it ensures there is no match for "arid".
To achieve whole-word matching, use the REGEXP operator with word boundary markers. The [[:<:]]
and [[:>:]]
tags define word boundaries.
<code class="language-sql">SELECT * FROM table WHERE keywords REGEXP '[[:<:]]rid[[:>:]]'</code>
For MySQL 8.0.4 and later, the standard word boundary tag b
should be used instead.
<code class="language-sql">SELECT * FROM table WHERE keywords REGEXP '\brid\b'</code>
Remember that for REGEXP mode to work properly, a backslash needs to be escaped with another backslash. By using this technique, you can perform exact whole-word matching in MySQL, ensuring the accuracy of your search results.
The above is the detailed content of How to Perform Exact Word Searches in MySQL?. For more information, please follow other related articles on the PHP Chinese website!