Search code examples
mysqlin-clause

MySQL IN clause EITHER value


I have this query:

SELECT 1 IN (1,2)

Which returns 1 since 1 is inside (1,2).

What I want to do is check if either one of two values exists in the array. In an imaginary world:

SELECT (3,1) EITHER IN (1,2)

Something like this should return 1 since at least one value was found in the second array. Of course this query is incorrect. Is there a way to do this and avoid this:

SELECT (
   3 IN (1,2)
   OR
   1 IN (1,2)
)

Solution

  • you can use an inner join for that

    select a, b form T1
    inner join T2 on (T1.a = T2.c or T1.b = T2.c)