Skip to content
Advertisement

SELECT WHERE multiple records don’t exist

I have the following two tables.

attempts:

attempt_tags:

I’m looking to select a record from the attempts table where (for example) tagid 2 and 11 are BOTH not present, so the result of the query in this example would return everything except for id 40.

I initially thought this question would be a good solution, but I’m not as convinced now, I imagine in the scenario where I need to select the results that don’t have many tagid‘s present (e.g. 2, 11, 15, 18, 20, 25 etc), this query would end up getting quite long.

I’m sure there’s a simple solution, but I’m just not seeing it at the moment.

Advertisement

Answer

One method uses not exists, twice:

EDIT:

You can also simplify this to:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement