MySQL in-operator must match all values?

寵の児 提交于 2019-11-27 14:50:59

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/7505045/mysql-in-operator-must-match-all-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!