Erik Erik - 2 months ago 17
MySQL Question

SQL group by query to find only enabled records

I have a MySQL table like the following, and I'd like to find the name of the persons which are completely disabled.

This is my table:

+----+----------+---------+
| id | name | enabled |
+----+----------+---------+
| 1 | person 1 | 1 |
| 2 | person 1 | 0 |
| 3 | person 2 | 0 |
| 4 | person 2 | 0 |
| 5 | person 3 | 1 |
| 6 | person 3 | 1 |
+----+----------+---------+


(the
enabled
column represents a boolean)

In this case, I only want to find person 2, because both records are disabled. I don't want to find person 1, because person 1 is still enabled in row 1, and person 3 is completely enabled, so that on should also be excluded.

Answer

You can do this with aggregation and a having clause:

select name
from t
group by name
having max(enabled) = 0;
Comments