brianlange Posted June 4, 2011 Share Posted June 4, 2011 I have a posts table, a category table and a join table. The relationship between posts and categories is many to many. I want to query all posts that are not assigned to any of a certain group of categories. The query below doesn't work because the result set is only matching records. I don't know whether a particular record does not match categories 21, 22, or 23. The only solution I have at the moment is too loop through every record and check for a matching record in the join table. This would work but it seems a little tedious to do. SELECT * FROM `wp_term_relationships` tr JOIN wp_posts p ON p.ID = tr.object_id WHERE p.post_status = "publish" AND tr.object_id NOT IN ( 21, 22, 23 ) Thanks, -Briano Quote Link to comment https://forums.phpfreaks.com/topic/238402-query-help/ Share on other sites More sharing options...
requinix Posted June 4, 2011 Share Posted June 4, 2011 SELECT whatever you want FROM the posts table WHERE the post ID NOT IN ( SELECT the post ID FROM the join table WHERE the category ID IN (the categories you don't want) ) Quote Link to comment https://forums.phpfreaks.com/topic/238402-query-help/#findComment-1225180 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.