Home > Database > Mysql Tutorial > How to Perform a Whole Word Search in MySQL?

How to Perform a Whole Word Search in MySQL?

Patricia Arquette
Release: 2025-01-13 22:58:43
Original
324 people have browsed it

How to Perform a Whole Word Search in MySQL?

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template