15

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?

Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
adp
  • 226
  • 2
  • 9
  • Please give your actual table structures including PK. – Martin Smith Sep 21 '11 at 18:53
  • please post your real code or at least what are table1 and table2. my first aproach makes me join twice the thread table. please read this post and anwser http://stackoverflow.com/questions/7492699/how-can-i-structure-a-query-to-give-me-only-the-rows-that-match-all-values-in-a-c/7493309#7493309 – Saic Siquot Sep 21 '11 at 18:58

1 Answers1

29
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
Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235