3

I've got the following table:

+--------+--------+
|  group |  user  |
+--------+--------+
|      1 |      1 |
|      1 |      2 |
|      2 |      1 |
|      2 |      2 |
|      2 |      3 |
+--------+--------+

I need to select group, containing both user 1 and 2 and only 1 and 2 (not 3 or 42).

I tried

SELECT `group` FROM `myTable` 
WHERE `user` = 1 OR `user` = 2 
GROUP BY `group`;

But that of course gives me groups 1 and 2 while group 2 contains also user 3.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
Roman Bekkiev
  • 3,010
  • 1
  • 24
  • 31

2 Answers2

2

One way

SELECT `group` 
FROM myTable
GROUP BY `group`
HAVING GROUP_CONCAT(DISTINCT `user` ORDER BY `user`) = '1,2';

SQL Fiddle

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
1

You may try this:

SELECT `group` 
FROM myTable
GROUP BY `group`
HAVING GROUP_CONCAT(DISTINCT `user` ORDER BY `user`) = '1,2';
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331