Home > Database > Mysql Tutorial > How Can I Find Rows in MySQL Whose Values Are Substrings of a Given Text?

How Can I Find Rows in MySQL Whose Values Are Substrings of a Given Text?

Linda Hamilton
Release: 2025-01-01 08:08:10
Original
752 people have browsed it

How Can I Find Rows in MySQL Whose Values Are Substrings of a Given Text?

MySQL: Finding Strings Contained Within a Query Text

The LIKE operator in MySQL is a powerful tool for searching rows that contain a specified substring. However, what if you need to perform a reverse search, finding rows that are contained within a query text?

For example, given the query string "John Smith and Peter Johnson are best friends," you might want to retrieve all names from a table that appear within this string.

To achieve this, you can utilize the CONCAT function to concatenate a wildcard character (%) with the name column and then use LIKE to search within the query string:

SELECT name FROM user 
WHERE 'John Smith and Peter Johnson are best friends' LIKE
  CONCAT('%', name, '%')
Copy after login

This query constructs a modified version of the query string by adding wildcards around each name in the table. The LIKE operator then compares this modified string to the original query string, returning names that are found within it.

The above is the detailed content of How Can I Find Rows in MySQL Whose Values Are Substrings of a Given Text?. 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