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, '%')
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!