Here is my moderate rate:
Examples of tables:
Parent
PID NAME 1 dad john 2 mum sandy 3 dad frank 4 mum kate 5 mum jean
Child
CID NAME 11 dave 22 maryam 33 henry 44 maryam 16 jill 17 lina 23 jack 34 jill 55 dave
Parent_child
PID CID 1 11 1 16 1 17 2 22 3 33 4 44 2 23 5 55 3 34
Query:
select p.pid, p.name, group_concat(c.name) as children from parent as p inner join parent_child as pc on p.pid = pc.pid join child as c on pc.cid = c.cid where c.name in ('dave','henry','maryam','jill') group by p.pid ;
Results:
PID NAME CHILDREN 1 dad john dave,jill 2 mum sandy maryam 3 dad frank henry,jill 4 mum kate maryam 5 mum jean dave
Using REGEXP and GROUP_CONCAT
This is much better than in or find_in_set for SQL. The change I made, I used the list as a comma delimitted string ;)
* But the problem is this: this string order group_concat should be found in a comma-separated string. * If we do not make REGEXP very effective :)
Query:
select x.pid, x.name, x.children from( select p.pid, p.name, group_concat(c.name) as children, count(c.name) as counts from parent as p inner join parent_child as pc on p.pid = pc.pid join child as c on pc.cid = c.cid group by p.pid) as x where 'dave,maryam,henry,jill' REGEXP x.children ;
Results:
PID NAME CHILDREN 3 dad frank henry,jill 4 mum kate maryam 5 mum jean dave
* SQLFIDDLE