2

This is a very basic query (related to this one) I can't figure out …

Let's say I have a two column table like this:

A  -  B
1  -  1
1  -  2
1  -  3
2  -  1
3  -  1
3  -  4

I want to get all distinct As that do not have a B of 2 or 3. Using the above example, the only results I want returned is As 2 and 3. How do I do this?

Community
  • 1
  • 1
idle
  • 155
  • 6

2 Answers2

4
SELECT DISTINCT `A` FROM `t` AS `t1`
    WHERE NOT EXISTS (
        SELECT 1 FROM `t` 
            WHERE `t`.`A` = `t1`.`A` 
            AND `B` in (2,3)
    );

SQL Fiddle demo

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
valex
  • 23,966
  • 7
  • 43
  • 60
0

try to use the following code:

select distinct a
from tbl
where B not IN(2,3)
Nikhil Doomra
  • 398
  • 2
  • 9
  • This would still return 1 for A because in the first row of A B is neither 2 nor 3 … – idle Aug 09 '13 at 16:26