Hi All
I'm struggling to understand the concepts of Counts and Maximums in MySQL. (or perhaps... my SQL skills...) I have the following SQL that works quite well:The above SQL shows all threads for a specific forum, the post count as well as the last post's title, user, and time created. (You can see it in action here)Code:SELECT T1.ROW_ID, T1.NAME AS THREAD, T2.NAME AS USER, T1.RATING, MAX(T3.CREATED_DATE) AS POSTCREATEDATE, T4.NAME AS LASTUSER, COUNT(T3.PAR_THREAD_ID) AS POSTCOUNT FROM BGF_THREADS T1 INNER JOIN BGF_USERS T2 ON T2.ROW_ID = T1.CREATED_BY INNER JOIN BGF_POSTS T3 ON T3.PAR_THREAD_ID = T1.ROW_ID INNER JOIN BGF_USERS T4 ON T4.ROW_ID = T3.CREATED_BY WHERE T1.PAR_FORUM_ID = '1' -- Parsed in with php GROUP BY T1.ROW_ID, T1.NAME, T2.NAME, T1.RATING ORDER BY POSTCREATEDATE DESC
Now I want to implement the same kind of action in the main forum page, though I can't seem to get it going...
This SQL gives me incorrect results. Somehow, it returns both the post count and the thread count exactly the same. (See the results here. There are no threads and posts on any forums except the first one. There should be two threads, four posts)Code:SELECT T1.ROW_ID, T2.NAME AS TYPE, T1.NAME AS FORUM, T1.DESC_TEXT, COUNT( T3.PAR_FORUM_ID ) AS THREADCOUNT, COUNT( T4.PAR_THREAD_ID ) AS POSTCOUNT, MAX( T4.CREATED_DATE ) AS LAST_POST FROM BGF_FORUMS T1 LEFT OUTER JOIN BGF_FORUMTYPE T2 ON T2.ROW_ID = T1.PAR_FORUMTYPE_ID LEFT OUTER JOIN BGF_THREADS T3 ON T3.PAR_FORUM_ID = T1.ROW_ID LEFT OUTER JOIN BGF_POSTS T4 ON T4.PAR_THREAD_ID = T3.ROW_ID GROUP BY T2.NAME, T1.NAME ORDER BY T2.NAME, T1.NAME
Can anyone explain why I am getting the incorrect thread count with the above SQL? I've been trying for hours now, but just can't get it working.
Any help would be greatly appreciated!![]()



Reply With Quote
