Select entries that do not have a specific value in the field.

For a table with fields A and B:

| A | B |
---------
| 1 | p |
| 1 | f |
| 1 | t |
| 2 | p |
| 2 | f |

I'm trying to build a query, find all A, which also does not have 't' for B.

So, for this data, the output should be

| A |
-----
| 2 |

since 2 does not have the 't' specified anywhere in field B

I tried to do SELECT DISTINCT A FROM table WHERE B!='t', but this logic is wrong, since 1 also contains a line with B!='t'. I also tried various options GROUP BY, but I was stuck.

Please, help

+4
source share
3 answers

I used to group by, and havingfor this purpose:

select a
from t
group by a
having sum(b = 't') = 0;
+5
source

Try the following:

SELECT A
FROM mytable
GROUP BY A
HAVING SUM(B = 't') = 0

HAVING A, B = 't'.

+4

Use the GROUP BY AND HAVING command:

 SELECT A
 FROM 
 GROUP BY A
 HAVING SUM(B = 't') = 0;
0
source

Source: https://habr.com/ru/post/1675514/


All Articles