Nilesh
Nilesh

Reputation: 313

Regular expression for MySQL database

I have a table emp_skills which has emp_id and skill_id as two columns. Now,

SELECT * FROM emp_skills where skill_id REGEXP '^[2,3]$' 

outputs emp_id's of those who have either 2 or 3 or both. What I want is emp_id's of those which have both 2 and 3 as their skill_id's. Skill_id is Integer

Example output of above query

EMP_ID     SKILL_ID
401        2   
405        2
401        3
405        3
407        3

Now what is I want is only first four rows as 407 does not have 2 as skill_id

Upvotes: 1

Views: 87

Answers (2)

John Woo
John Woo

Reputation: 263723

from your phrase both 2 and 3 as their skill_id, i think you don't need regex for this. All you need are only count, group by and having

SELECT EMP_ID
FROM emp_skills
WHERE skill_ID IN (2,3)     
GROUP BY EMP_ID
HAVING COUNT(EMP_ID) = 2    -- the total number of skill_ID

you need to match the number of records with the total number of id supplied in the where clause.

SQLFiddle Demo

Upvotes: 3

Omesh
Omesh

Reputation: 29091

Assuming skill_id as INTEGER column, then this can be easily achieved by using IN clause:

SELECT * 
FROM emp_skills 
WHERE skill_id IN(2,3);

Upvotes: 0

Related Questions