Hi there I'm having trouble a with a forum query
here are my 3 tables
categories
category_id
category_name
category_description
threads
thread_id
category_id
thread_title
thread_text
posts
post_id
thread_id
post_text
I want a query that selects all of the top level categories and displays them however in that query I am aware that I can generate the number of threads and number of posts each category has so when presented to the end user it will have the following headings
Name, description, # threads, # posts
This is what I have written so far which i thought worked untill i added another category to find that it doesn't
SELECT *,
DATE_FORMAT(last_post_datetime,\'%d-%m-%Y , %h:%i %p\') as category_datetime
FROM categories c
INNER JOIN (
SELECT t.category_id,count(*) as num_threads
FROM threads t
GROUP BY t.category_id
) as NT ON NT.category_id = c.category_id
INNER JOIN (
SELECT p.thread_id,count(*) as num_posts
FROM posts p
GROUP BY p.thread_id
) as NP ON NP.thread_id = NT.category_id
ORDER BY last_post_datetime DESC
LIMIT 5
Any help would be much appreciated
thanks