Home > Database > Mysql Tutorial > How Can I Achieve Whole Word Matching in MySQL Searches?

How Can I Achieve Whole Word Matching in MySQL Searches?

Susan Sarandon
Release: 2025-01-14 06:13:48
Original
329 people have browsed it

How Can I Achieve Whole Word Matching in MySQL Searches?

Precise Keyword Searches in MySQL Using Regular Expressions

Improving Search Accuracy with Regular Expressions

For precise whole-word matching in MySQL queries, leverage the REGEXP operator in conjunction with word boundary markers. This ensures your keywords only match when they're independent words, preventing partial matches within larger words.

Older MySQL Versions (Pre-8.0.4):

<code class="language-sql">SELECT *
FROM table 
WHERE keywords REGEXP '[[:<:]]rid[[:>:]]'</code>
Copy after login

The [[:<:]] and [[:>:]] markers define word boundaries.

MySQL 8.0.4 and Later:

MySQL versions 8.0.4 and later use a different regular expression engine. The recommended approach now is to use the standard word boundary marker b:

<code class="language-sql">SELECT *
FROM table 
WHERE keywords REGEXP '\brid\b'</code>
Copy after login

Note the double backslash (\) escaping the backslash character. This is crucial for correct interpretation by the MySQL engine.

The above is the detailed content of How Can I Achieve Whole Word Matching in MySQL Searches?. 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