i have the following query that pulls posts that belong to a blog
SELECT p.*
FROM `Posts` p
INNER JOIN Blogs b ON b.id = p.blog_id
Is there a MySQL function that does the opposite of “ON”? If i wanted to query posts that didn’t belong to a blog in the “Blogs” database table? For instance if the blog was deleted.
It’s all about the JOIN.
Should do the trick for you.
Jeff Atwood has a great overview of joins
In this instance, what the LEFT OUTER JOIN does is match all rows in Blogs with all rows in Posts with the same blog_id-Id combo AND all rows in Posts that have no matching Id in Blogs. In this instance, those rows are NULL for the entries in Blogs (as there aren’t any!), which is why we filter on
b.Id IS NULLThere are some other ways you can achieve this too; for example:
Effectively, this queries all rows in posts that have a blog_id that isn’t in the blog table.