Reputation: 227
I have this table: And I would like to get the IDs, that don´t have the language english.
+----+----------+-------+
| ID | LANGUAGE | NAME |
+----+----------+-------+
| 1 | english | james |
+----+----------+-------+
| 1 | french | john |
+----+----------+-------+
| 2 | french | ted |
+----+----------+-------+
| 3 | german | tom |
+----+----------+-------+
| 3 | english | james |
+----+----------+-------+
| 4 | spanish | lucy |
+----+----------+-------+
| 4 | german | Bud |
+----+----------+-------+
The output should be something like:
+----+----------+-------+
| ID | LANGUAGE | NAME |
+----+----------+-------+
| 2 | french | ted |
+----+----------+-------+
| 4 | spanish | lucy |
+----+----------+-------+
| 4 | german | Bud |
+----+----------+-------+
Upvotes: 1
Views: 1333
Reputation: 3807
The subquery builds a list of the id fields of all records where the language is English.
Using that, we find all records in the table which have id fields that are not in that list. So those are the records for ids where there is no English entry.
SELECT * FROM table
WHERE table.id NOT IN (SELECT id FROM table WHERE language='english');
Upvotes: 1
Reputation: 164064
With NOT EXISTS:
select * from tablename t
where not exists (
select 1 from tablename
where id = t.id and language = 'english'
)
Upvotes: 2