I have two tables A and B. A is the parent of B. I am trying to find all As that do not have a specific B as a child. I would usually do
SELECT A.id FROM A
WHERE A.id NOT IN
(SELECT B.AId FROM B WHERE B.someFK = foo);However, for performance reasons, I try not to use internal selection. I tried something like:
SELECT A.id FROM A
LEFT JOIN B ON (A.id = B.AId)
WHERE B.someFK! = Foo OR B.someFK IS NULLThe problem is that it returns as having more than one child, regardless of whether they have the specified B.
EDIT: Changed B.id to B.someFK
source
share