0

hello all i am working on a project where i need to select the field only if it contains any of the values of an array

what i mean is that

i have a string like (comma separated)

 $string =1,2,3,4,5,7;

and i have a database which contains a table wtth values like

 1,9 // this is one of the field which conatins the id of people who should be notified

now i want to select all the fields which contains any of the value out of the string like

if the string is like

  $string =1,7;

and

 field contains 1,8
 filed2 contains 9,88
 field3 contains 2,5,6,7,8

then select only 1 and 3 filds

i have tried this

      select aid,a.cid,tdid,fdid,mcat,sub,a.mess,attach,prio,sdate,edate,stime,etime,a.date,d.name,d.photo,c.curl,c.cname 
 from activity a,dept d,enter c where 
 ((CONCAT(",", tdid, ",") REGEXP ",('.implode("|",$arr).'),") and tdid=d.did and d.acid=c.cid)
 $mydeptslist=1,8 
 $arr=array($mydeptslist);

this code dosent select anything even if the value matches .. please suggest me something else .... i have a dynamic string..

sonam Sharma
  • 548
  • 12
  • 30
  • Easy. See normalisation – Strawberry Nov 22 '15 at 14:09
  • give a sample data along with your query -- Use [SQL Fiddle](http://sqlfiddle.com/) – Supun Silva Nov 22 '15 at 14:10
  • If you could normalize the database table then it would be just a simple query using an `IN` test? maybe interesting? [Can I resolve this with pure mysql? (joining on ; separated values)](http://stackoverflow.com/a/33806675/3184785) – Ryan Vincent Nov 22 '15 at 18:51

1 Answers1

0

The MySQL function find_in_set() can search only for one string in a set of strings.You can try something like this

find_in_set('1',comma_sep_field_name) OR find_in_set('7', comma_sep_field_name) 
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103