Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL in-operator must match all values?

I've made my own forum. When doing a search I want to find any threads where two (or more) specific users have participated. I came up with this:

SELECT * FROM table1 INNER JOIN table2 
ON table1.threadid=table2.threadid 
WHERE table2.threadcontributor IN ('1','52512')

Before realizing that it actually means '1' OR '52512'.

Is there any way to make it work so that all id's has to match?

like image 445
adp Avatar asked Sep 21 '11 18:09

adp


1 Answers

SELECT * 
    FROM table1 
        INNER JOIN table2 
            ON table1.threadid=table2.threadid 
    WHERE table2.threadcontributor IN ('1','52512')
    GROUP BY table1.PrimaryKey
    HAVING COUNT(DISTINCT table2.threadcontributor) = 2
like image 125
Joe Stefanelli Avatar answered Oct 14 '22 18:10

Joe Stefanelli