Assistance with a tuf query

The following query return a list of forums and related info.
I need the query to return the number of threads in each forums and the total number of messages from all threads in that forum.
The table name for the threads is "sf_threads" and for the messages "sf_messages".
Thanks for the help. Hopefully I will learns from your answers.

A.conferenceidfk, as conference,
B.sort_order as conference_order,
A.sort_order as forum_order,
E.title as group_title,
A.msgcount as messagecount,

(forums A
inner join conferences B
on A.conferenceidfk =
left join usergroups E

where      1=1

order by
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian M.Web DeveloperCommented:
Would something like this work? :

SELECT, COUNT( AS num_threads, COUNT( AS num_messages
FROM forums
LEFT JOIN sf_threads AS threads
ON = threads.forum_id
LEFT JOIN sf_messages AS messages
ON = messages.forum_id;
Hi contiw,

I would do something like below to combine your query and additional requirements, I am guessing at the names of the columns in the message/thread tables are forumid but this is probably wrong and needs to be updated with the correct ones.
	, A.description
	, A.readonly
	, A.membersonly
	, A.conferenceidfk
	, as conference
	, B.sort_order as conference_order
	, A.sort_order as forum_order
	, A.groupidfk
	, E.title as group_title
	, A.msgcount as messagecount
	, A.lastpost
	, T.threadCount
	, M.messageCount
	forums A 
	join conferences B on A.conferenceidfk =
	left join usergroups E on A.groupidfk =
	left join (SELECT forumid, COUNT(*) as threadCount FROM sf_threads GROUP BY forumid) T on A.ForumID = T.ForumID
	left join (SELECT forumid, COUNT(*) as messageCount FROM sf_messages GROUP BY forumid) M on  A.ForumID = M.ForumID
order by
	, A.sort_order

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jinesh KamdarCommented:
Try this. Also, wat does the "msgcount" field in the forums table hold ?
FROM forums f, sf_threads t, sf_messages m
WHERE = t.forum_id
AND = m.thread_id

Open in new window

contiwAuthor Commented:
Thank you macmonap. Excellent albeit extremely slow when using a 9600 messages. So I decided to add a column to keep a running count always updated for threads and messages. Your answer cannot be improved. Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.