I have this code:
$result = mysql_query("SELECT
f_type.`id` AS type_id,
f_type.`name` AS type_name,
f_thread.`id` AS thread_id,
f_thread.`uid`,
f_thread.`title` AS thread_title,
f_thread.`hits`,
u.`username`
FROM `forum_type` AS f_type
LEFT JOIN `forum_thread` AS f_thread
ON f_type.`id` = f_thread.`type_id`
LEFT JOIN `users` AS u
ON u.`id` = f_thread.`uid`
LEFT JOIN `forum_posts` AS f_posts
ON f_posts.`type_id` = f_thread.`id`
WHERE f_type.`id` = '".$_GET['type_id']."'
ORDER BY f_posts.`date` DESC
") or die (mysql_error());
It’s about a forum and I want the threads to be sorted like checking which post is the newest in this forum.
I use forum_type (f_type) as a sub-category of forum, which is the top-category. So it’s hierarchy is like this list:
- forum
- forum_type
- forum_thread
- forum_posts
And like in the query I’ve written in the code, I wanted to order by the newest post date… but it doesn’t order by that. I don’t know actually what it’s ordered by… can’t see a scheme. I guess this problem’s caused, because of these multiple joins, but I’m not sure.
I would appreciate any suggestions!
Well, you might want to group by threads and select the maximum post date for each thread if you want to list threads.
Hope that helps. f_posts.type_id is a bit suspect, maybe there’s a thread_id instead in the table?