{"id":1128,"date":"2017-02-20T23:01:00","date_gmt":"2017-02-20T17:31:00","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1128"},"modified":"2026-09-01T15:44:28","modified_gmt":"2026-09-01T10:14:28","slug":"mysql-find_in_set-multiple-search-string","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/mysql-find_in_set-multiple-search-string\/","title":{"rendered":"MySQL FIND_IN_SET with Multiple Search Strings"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This post explains how to search for multiple strings in MySQL without using the <strong><code>FIND_IN_SET()<\/code><\/strong> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In MySQL, the <code>FIND_IN_SET()<\/code> function can be used to search for a single string within a comma-separated list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">find_in_set('a','a,b,c,d')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, searching for multiple values directly like the following is not possible:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">find_in_set('a,b,c,d','a,b,c,d')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If we want to search for multiple values using <code>FIND_IN_SET()<\/code>, we need to use multiple conditions:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">find_in_set('a', 'a,b,c,d') OR find_in_set('b', 'a,b,c,d') OR find_in_set('c', 'a,b,c,d')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using this format can become difficult to implement in multiple places. The following approach provides a simple way to search for multiple values without using the <code>FIND_IN_SET()<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before implementing the query, you should have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>MySQL installed and configured.<\/li>\n\n\n\n<li>Access to the MySQL database.<\/li>\n\n\n\n<li>A table containing the values you want to search.<\/li>\n\n\n\n<li>Basic knowledge of SQL queries.<\/li>\n\n\n\n<li>Basic knowledge of PHP if using the PHP example.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Search Multiple Values Using MySQL<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The following MySQL query can be used to search for multiple values without using the <code>FIND_IN_SET()<\/code> function:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT * from table_name WHERE CONCAT(\",\", `id`, \",\") REGEXP \",(1|2|3),\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the <code>REGEXP<\/code> expression allows multiple values to be specified using the <code>|<\/code> operator.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1|2|3<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">means that the query searches for <code>1<\/code>, <code>2<\/code>, or <code>3<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Using FIND_IN_SET() for Comparison<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With <code>FIND_IN_SET()<\/code>, multiple search values would require separate conditions:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">find_in_set('a', 'a,b,c,d') OR find_in_set('b', 'a,b,c,d') OR find_in_set('c', 'a,b,c,d')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The alternative query avoids repeating <code>FIND_IN_SET()<\/code> for every search value:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT * from table_name WHERE CONCAT(\",\", `id`, \",\") REGEXP \",(1|2|3),\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This can make the query easier to construct when multiple values need to be searched.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PHP with MySQL Without FIND_IN_SET()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the search values are available as an array in PHP, the query can be constructed dynamically.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Assume the following array values:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$a=array(10,12,13,14,15);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The query can be generated using the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\n$a=array(10,12,13,14,15);\n$test=\"SELECT * from table_name WHERE\";\n$tot=count($a);\n$counter=1;\nforeach($a as $val)\n{\n echo $counter;\n $test .= \" id=$val\";\n if($counter !=$tot)\n {\n $test .=\" OR \";\n }\n $counter++;\n} \necho $test;\n mysql_query($test); ?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The PHP code loops through the array values and adds each value to the SQL query using an <code>OR<\/code> condition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, the generated query will look similar to:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT * from table_name WHERE id= 10 OR id = 12 OR id= 13 ...<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After the query is generated, it can be executed to retrieve the matching results.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL&#8217;s <code>FIND_IN_SET()<\/code> function is useful for searching a single value in a comma-separated list, but searching multiple values can require multiple <code>FIND_IN_SET()<\/code> conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using <code>CONCAT()<\/code> with <code>REGEXP<\/code> provides another approach for searching multiple values, while PHP can be used to dynamically build an SQL query when the values are available in an array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">What is <code>FIND_IN_SET()<\/code> in MySQL?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>FIND_IN_SET()<\/code> is a MySQL function used to search for a string within a comma-separated list of values.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Can <code>FIND_IN_SET()<\/code> search multiple values directly?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">No. Multiple values generally require separate <code>FIND_IN_SET()<\/code> conditions connected using operators such as <code>OR<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How can I search multiple values without <code>FIND_IN_SET()<\/code>?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">You can use <code>CONCAT()<\/code> &#8220;together with&#8221; <code>REGEXP<\/code> to match multiple values in the query.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Can PHP arrays be used to build a MySQL query?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. PHP arrays can be processed using a loop to construct multiple SQL conditions dynamically.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Why use <code>REGEXP<\/code> multiple search values?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>REGEXP<\/code> allows multiple matching alternatives to be specified using the <code>|<\/code> operator, making it possible to search for several values in a single expression.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Change WordPress Site URL from Backend Using MySQL\/MariaDB<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Learn how to change the WordPress site URL directly from the MySQL\/MariaDB database when the site URL needs to be updated from the backend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/pheonixsolutions.com\/blog\/change-wordpress-site-url-backendmysql-mariadb\/?utm_source=chatgpt.com\">Change WordPress Site URL Backend Using MySQL\/MariaDB<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">MySQL FIND_IN_SET with Multiple Search Strings<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This article provides another reference for working with MySQL <code>FIND_IN_SET()<\/code> and handling multiple search strings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/mysql-find_in_set-multiple-search-string\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL FIND_IN_SET Multiple Search Strings<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This post explains how to search for multiple strings in MySQL without using the FIND_IN_SET() function. In MySQL, the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[273],"class_list":["post-1128","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-ic","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1128"}],"version-history":[{"count":3,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1128\/revisions"}],"predecessor-version":[{"id":11286,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1128\/revisions\/11286"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}