use test;
create table MyTable
( `uid` varchar(8),
`c1` varchar(8),
`c2` varchar(8)
);
insert into MyTable (uid, c1, c2) values('111', 'aaa', 'c');
insert into MyTable (uid, c1, c2) values('111', 'ccc', 'c');
insert into MyTable (uid, c1, c2) values('111', 'ddd', 'c');
insert into MyTable (uid, c1, c2) values('222', 'bbb', 'c');
insert into MyTable (uid, c1, c2) values('222', 'ddd', 'c');
insert into MyTable (uid, c1, c2) values('222', 'eee', 'c');
insert into MyTable (uid, c1, c2) values('333', 'ddd', 'c');
insert into MyTable (uid, c1, c2) values('333', 'eee', 'c');
insert into MyTable (uid, c1, c2) values('333', 'fff', 'c');
insert into MyTable (uid, c1, c2) values('444', 'xxx', 'd');
insert into MyTable (uid, c1, c2) values('444', 'yyy', 'd');
insert into MyTable (uid, c1, c2) values('444', 'zzz', 'd');
select count(distinct uid) from MyTable where (c1 like 'a%' or c1 like 'b%') and c2='c';
select count(distinct uid) from MyTable where c2='c';
mysql> select count(distinct uid) from MyTable where (c1 like 'a%' or c1 like 'b%') and c2='c';
+---------------------+
| count(distinct uid) |
+---------------------+
| 2 |
+---------------------+
1 row in set (0.00 sec)
mysql> select count(distinct uid) from MyTable where c2='c';
+---------------------+
| count(distinct uid) |
+---------------------+
| 3 |
+---------------------+
SELECT DISTINCT UID
FROM MyTable x
WHERE NOT EXISTS (
SELECT 1 from MyTable y
WHERE (c1 LIKE 'a%' or c1 LIKE 'b%') and c2='c'
AND x.uid = y.uid
);
+------+
| UID |
+------+
| 333 |
| 444 |
+------+
This should only return 333