Home >Database >Mysql Tutorial >How Can I Perform Multiple String Searches Using MySQL's find_in_set?
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!