Home >Database >Mysql Tutorial >How Can I Perform Multiple String Searches Using MySQL's find_in_set?

How Can I Perform Multiple String Searches Using MySQL's find_in_set?

Susan Sarandon
Susan SarandonOriginal
2025-01-11 19:51:47243browse

How Can I Perform Multiple String Searches Using MySQL's find_in_set?

Multi-string search techniques for MySQL find_in_set function

Question:

MySQL’s find_in_set function can search for a single string in a comma-separated value (CSV) collection. But how do you take advantage of its capabilities to search for multiple strings at the same time?

Answer:

Although MySQL does not have native functions to support multi-string search, you can use the following techniques:

<code class="language-sql">WHERE CONCAT(",", `setcolumn`, ",") REGEXP ",(val1|val2|val3),"</code>

This method concatenates the target CSV collection with commas, including leading and trailing commas. Then, check whether the resulting string matches any desired search string pattern separated by a pipe symbol (|). For example:

<code class="language-sql">WHERE CONCAT(",", `tags`, ",") REGEXP ",(red|green|blue),"</code>

This query will return those rows where the tags column contains any of the values ​​"red", "green", or "blue".

The above is the detailed content of How Can I Perform Multiple String Searches Using MySQL's find_in_set?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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