Pankaj Sharma
Pankaj Sharma

Reputation: 33

How to search from comma separated string in comma separated column mysql

I am working on a project where I have to code for job filters. In this when I filters for salaries it sends salary id's in array like:

job_salary = array(
              [0]=>3,
              [1]=>4,
              [2]=>5,
              [3]=>6,
              [4]=>7,
              [5]=>8  
              )

and job in my jobs table contains multiple salaries using comma string.

id  salary_ids 
 1    2,4,6
 2    1,3,5
 3    4,5
 4    9,1

So if i search for value 3, and 5 in array form like

job_salary = array(
              [0]=>3,
              [1]=>5
              );

It should return me the second and third row because these rows contain value 3 and 5.

Upvotes: 0

Views: 3512

Answers (2)

Saranga  kapilarathna
Saranga kapilarathna

Reputation: 644

Please try again like this

  1. For Postgres & MySQL:

    SELECT FIND_IN_SET(5, userid) AS result 
    FROM department
    
  2. For PostgreSQL:

    SELECT * 
    FROM TABLENAME f
    WHERE 'searchvalue' = ANY (string_to_array(COLUMNNAME,','))
    

Example

select * 
from customer f
where '11' = ANY (string_to_array(customerids,','))

Upvotes: 0

Swathi Kottakota
Swathi Kottakota

Reputation: 24

Use it like below let me know if it works

$values=array("1","2","3");

foreach($values as $val)
{
    $query="Select * from table_name where  FIND_IN_SET('".$val."',column_name)";
$result=mysql_query($query);
$data=mysql_fetch_array($result);
$dbval[]=$data['column_name'];

}

print_r($dbval);

Upvotes: 1

Related Questions