0

我有这个代码:

$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) 作为论坛的子类别,它是顶级类别。所以它的层次结构就像这个列表:

  1. 论坛
  2. 论坛类型
  3. 论坛线程
  4. 论坛帖子

就像我在代码中编写的查询一样,我想按最新的发布日期排序......但它没有按那个排序。我实际上不知道它是由什么订购的……看不到计划。我猜这个问题是由这些多重连接引起的,但我不确定。

我将不胜感激任何建议!

4

1 回答 1

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 代替?

于 2012-09-16T15:20:51.083 回答