Select values that meet different conditions on different rows?
问题 This is a very basic query I can\'t figure out.... Let\'s say I have a two column table like this: userid | roleid --------|-------- 1 | 1 1 | 2 1 | 3 2 | 1 I want to get all distinct userids that have roleids 1, 2 AND 3. Using the above example, the only result I want returned is userid 1. How do I do this? 回答1: SELECT userid FROM UserRole WHERE roleid IN (1, 2, 3) GROUP BY userid HAVING COUNT(DISTINCT roleid) = 3; To anyone reading this: my answer is simple and straightforward, and got the