Here is the scenario. How to utilize REGEXP
to simulate IN
operator yet to match all values in left side within right side regardless of the string order in either side. Also ANSI SQL
solution can be achieved using left join and sub queries.
Sample table:
Parent table, Child table, Parent_Child
. In order to not take more space on the question, I only post here a reulst of a Group_Concat Child query by Parent
.
PID NAME CHILDREN
1 dad john dave,jill,lina
2 mum sandy maryam,jack
3 dad frank henry,jill
4 mum kate maryam
5 mum jean dave
Expected Result: Select Parent who has their all children participated in something.
PID NAME CHILDRENREXGEX
3 dad frank jill,henry
4 mum kate maryam
5 mum jean dave
Here is the REGEXP SQL solution: now the issue here, it doesn't return correct results if left side order/squence not match right side.
Query:
select
x.pid, x.name, x.children as childrenRexgex
from
(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
group by
p.pid
order by
c.name) as x
where
'dave,maryam,jill,henry' REGEXP x.children
;
Hence there are two aspects I would appreciate for the question:
- What is the best pattern to match all names in left side to the user defined list of right side regardless of the order?
- What could be the performance gained using
REGEXP
?