I am trying to come up with a query similar to an IN (1,2,3) statement, but that MUST match all criteria in a one to many relationship, rather than just some of.
I have a working example of what I am looking for, but it just seems wrong to me, and as the queries are built up dynamically (and part of a much larger query), it could run into problems with large amounts of criteria.....
The example returns the id's of all people who have ALL the required skills..
SELECT P.PERSON_ID
FROM PEOPLE P
WHERE P.PERSON_ID in (
SELECT DISTINCT s1.PERSON_ID
FROM SKILLS s1
INNER JOIN SKILLS s2 ON s1.PERSON_ID = s2.PERSON_ID
INNER JOIN SKILLS s3 ON s1.PERSON_ID = s3.PERSON_ID
WHERE s1.SKILL = 'Skill 1'
AND s2.SKILL = 'Skill 2'
AND s3.SKILL = 'Skill 3'
)
This seems to be a case of overkill, so is there a simpler, and more scaleable way to do what I'm looking for?? I just can't see a different way of doing it, but am far from being any kind of SQL expert...
Thanks,
<< JOC >>
I have a working example of what I am looking for, but it just seems wrong to me, and as the queries are built up dynamically (and part of a much larger query), it could run into problems with large amounts of criteria.....
The example returns the id's of all people who have ALL the required skills..
SELECT P.PERSON_ID
FROM PEOPLE P
WHERE P.PERSON_ID in (
SELECT DISTINCT s1.PERSON_ID
FROM SKILLS s1
INNER JOIN SKILLS s2 ON s1.PERSON_ID = s2.PERSON_ID
INNER JOIN SKILLS s3 ON s1.PERSON_ID = s3.PERSON_ID
WHERE s1.SKILL = 'Skill 1'
AND s2.SKILL = 'Skill 2'
AND s3.SKILL = 'Skill 3'
)
This seems to be a case of overkill, so is there a simpler, and more scaleable way to do what I'm looking for?? I just can't see a different way of doing it, but am far from being any kind of SQL expert...
Thanks,
<< JOC >>