How to check if multiple column values exists in another array?

Viewed 51

I hope I will explain my problem

I have table something like this:

id|id_2 (table test)
1,2
1,3
1,4
2,3
2,4

now i have array of values (id_2's) [2,3,4].

How to get only id's (from table test) where ALL id_2 columns are in array [2,3,4]?

So, in my example, i need value id=1 because id=2 doesn't have value 2.

1 Answers
SELECT DISTINCT(t.id) FROM test t WHERE t.id_2 IN 
     (SELECT DISTINCT(t2.id_2) FROM test t2);

This query would solve your problem.

Related