MySQL Forums
Forum List  »  Newbie

What is the Correct SQL - Need to Join 3 Tables
Posted by: Stefanie Steckerl
Date: March 29, 2005 11:51AM

I have 3 tables: wp_categories, wp_post2cat and wp_posts. I am trying to display the category names (cat_name) from wp_categories for each distinct occurrence of category_id from wp_post2cat where the value mem_level is <= 1 and the post_status is = "publish" from the wp_posts table. In wp_categories, the primary key is cat_ID for each cat_name. The primary key in wp_post2cat is post_id. In wp_posts, the primary key is ID. This table contains all the posts with the values for mem_level as well as post_status. The other thing I need to do is display a count of the number of distinct category_ids in wp_post2cat where the value mem_level is <= 1 and the post_status is = "publish" in the wp_posts table. I thought the following SQL might work, however, it doesn't bring back any results - can anyone help me with this?:
SELECT cat_name,
COUNT(post2cat.category_id) AS cat_count
FROM wp_categories
INNER JOIN post2cat ON (cat_ID = category_id)
INNER JOIN wp_posts ON (ID = post_id)
WHERE post_status = 'publish'
AND post_date_gmt < '$now' $exclusions
GROUP BY category_id
Any help would be greatly appreciated. Thank you!

Options: ReplyQuote


Subject
Written By
Posted
What is the Correct SQL - Need to Join 3 Tables
March 29, 2005 11:51AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.