Есть о чем подумать:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(name CHAR(1) NOT NULL
,number INT NOT NULL
,PRIMARY KEY(name,number)
);
INSERT INTO my_table VALUES
('A',1),
('A',2),
('B',1),
('C',1),
('D',1),
('D',2),
('D',3);
SELECT *, number IN (1,2) x FROM my_table;
+------+--------+---+
| name | number | x |
+------+--------+---+
| A | 1 | 1 |
| A | 2 | 1 |
| B | 1 | 1 |
| C | 1 | 1 |
| D | 1 | 1 |
| D | 2 | 1 |
| D | 3 | 0 |
+------+--------+---+
SELECT name
, COUNT(*)
, SUM(number IN (1,2)) x
FROM my_table
GROUP
BY name;
+------+----------+------+
| name | COUNT(*) | x |
+------+----------+------+
| A | 2 | 2 |
| B | 1 | 1 |
| C | 1 | 1 |
| D | 3 | 2 |
+------+----------+------+
... и на всякий случай, если это нужно разобрать ...
SELECT name
FROM my_table
GROUP
BY name
HAVING COUNT(*) = 2 -- DISTINCT is redundant because of my PRIMARY KEY
AND COUNT(*) = SUM(number IN (1,2));
;
+------+
| name |
+------+
| A |
+------+