Considering the sample table below:
Profile_ID Child_ID
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
3 5
I want to construct an SQL query which gives me the profile IDs which are applicable to children 1 AND 2 AND 3... this implies that profile_ID 2 will not be in the result as profile 2 is only applied to children 1 and 2.. but I expect that profile 3 is included.
My idea is something similar to this
SELECT Profile_ID
FROM table
WHERE Child_ID IN (1 AND 2 AND 3)
Now, I am well aware that IN
serves as an OR
function...
Is there a similar way to achieve similar functionality to the IN
clause with an AND
rather than an OR
?
Thanks in advance for all your help.
Try this:
select profile_id from t
where child_id in (1, 2 ,3)
group by profile_id
having count(distinct child_id) = 3
Here is the fiddle to play with.
NOTE: Make sure 3
matches the amount of items in the in
clause.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With