What I've been doing is
SELECT * FROM a LEFT JOIN b ON b.a_id=a.id WHERE b.id IS NULL
Basically, I'm trying to find the rows of a
that don't have an associated b
, where the foreign key is stored on b
. Is this the proper way to do it, or there a different kind of join to do this?
You are looking for NOT EXISTS
:
SELECT
*
FROM
a
WHERE
NOT EXISTS (SELECT 1 FROM b WHERE a_id = a.id)
Having an index on b.a_id
helps the performance of this query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With