Posted Date:20-02-2017

This Post will explain how to search multiple string in mysql without using find_in_set function.

In mysql find_in_set function possible check only string like this

Example:

find_in_set('a','a,b,c,d')

But not possible Search like this

find_in_set('a,b,c,d','a,b,c,d')

if the we want multiple search the text only possible like this

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')

In above format is very difficult to implement to all the places but we will use the given following trick  easily to achieve your aim

 This is an Mysql Query

The following query to use and get the result easily without find_in_set function

SELECT * from table_name WHERE CONCAT(",", `id`, ",") REGEXP ",(1|2|3),"

PHP WITH Mysql Without Find_in_set

Example we have array value but we want results based on array value that time implement this code and get result

Assumption value:

a[]=array(10,12,13,14,15);

 

<?php

$a=array(10,12,13,14,15);
$test="SELECT * from table_name WHERE";
$tot=count($a);
$counter=1;
foreach($a as $val)
{
 echo $counter;
 $test .= " id=$val";
 if($counter !=$tot)
 {
 $test .=" OR ";
 }
 $counter++;
} 
echo $test;
 mysql_query($test); ?>

Finally we getting output like this

SELECT * from table_name WHERE id= 10 OR id = 12 OR id= 13  ..etc;

after we execute query easily.

 

3 thoughts on “MySQL find_in_set with multiple search string”

  1. Your PHP WITH Mysql Without Find_in_set example can be done more efficiently using the array implode function in conjunction with array_map function.

    $a = array(10,12,13,14,15);
    $test = “SELECT * from table_name WHERE “.implode(” OR “, array_map(function($value,$index){ return “id = “.$value;},$a));

Leave a Reply