Hi folks -
I have a forum that uses two tables to maintain the threads and messages and I want to list all the threads in descending order of the most recent post.
Below is the query I have been using. I think the naming makes it obvious what's what, but basically there is a table called "Thread" that contains column "id" and some other stuff not relevant here. And there is a table called "Msg" with id, threadid (referring to Thread's id column), date, and so on. All Msg rows refer to some Thread.id.
I'm not well-versed on using GROUP BY, especially with JOINed tables. So I defer to the experts here.
The incorrect query:
SELECT DISTINCT Thread.id, count(*) as msgcount
FROM Thread
LEFT OUTER JOIN Msg ON Msg.threadid = Thread.id
GROUP BY Thread.id DESC
ORDER BY Msg.date DESC
I could probably do an unsorted query selecting the Thread.id and max(Msg.date), dumping it into an array, sorting the array, and so on, but I would like to know how to do it in MySQL for present and future use.
Thanks!
Jim
I have a forum that uses two tables to maintain the threads and messages and I want to list all the threads in descending order of the most recent post.
Below is the query I have been using. I think the naming makes it obvious what's what, but basically there is a table called "Thread" that contains column "id" and some other stuff not relevant here. And there is a table called "Msg" with id, threadid (referring to Thread's id column), date, and so on. All Msg rows refer to some Thread.id.
I'm not well-versed on using GROUP BY, especially with JOINed tables. So I defer to the experts here.
The incorrect query:
SELECT DISTINCT Thread.id, count(*) as msgcount
FROM Thread
LEFT OUTER JOIN Msg ON Msg.threadid = Thread.id
GROUP BY Thread.id DESC
ORDER BY Msg.date DESC
I could probably do an unsorted query selecting the Thread.id and max(Msg.date), dumping it into an array, sorting the array, and so on, but I would like to know how to do it in MySQL for present and future use.
Thanks!
Jim