我有这段代码:
$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());
关于一个论坛,我想要对线程进行排序,就像检查哪个帖子是本论坛中最新的一样。
我使用forum_type(f_type)作为论坛的子类别,这是最高类别。所以它的层次结构就像这个列表:
就像我在代码中写的查询一样,我想按最新的发布日期订购......但它并没有按顺序排序。我实际上并不知道它所订购的是什么......无法看到一个方案。我想这个问题是由于这些多次连接引起的,但我不确定。
我会很感激任何建议!
答案 0 :(得分:1)
好吧,您可能希望按线程分组,如果要列出线程,请选择每个线程的最大发布日期。
SELECT
f_thread.`id` AS thread_id,
f_thread.`uid`,
f_thread.`title` AS thread_title,
f_thread.`hits`,
u.`username`,
max(f_posts.date) as last_post_date
FROM `forum_thread` AS f_thread
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_thread.type_id = ".$_GET['type_id']."
GROUP BY thread_id
ORDER BY last_post_date DESC
希望有所帮助。 f_posts.type_id有点怀疑,也许在表中有一个thread_id?