我正在开发一个自定义论坛,我注意到我做错了什么。我根据线程日期对线程进行排序。我需要根据线程中最后一篇帖子的日期来组织线程。
我尝试了以下但是它有问题。它多次列出条目。
$thread_sql = "SELECT forum_threads.thread_id AS thread_id,
forum_threads.thread_title AS thread_title,
forum_threads.thread_description AS thread_description,
forum_threads.forum_id AS forum_id,
forum_threads.thread_postdate AS thread_date,
forum_threads.thread_icon AS thread_icon,
forum_threads.thread_views AS thread_views,
forum_threads.user_id AS user_id,
users.user_firstname AS user_firstname,
users.user_lastname AS user_lastname
FROM forum_threads
INNER JOIN users ON forum_threads.user_id = users.user_id
INNER JOIN forum_posts ON forum_threads.thread_id = forum_posts.thread_id
WHERE forum_threads.forum_id='$_GET[f]'
ORDER BY forum_posts.post_date DESC ";
关于如何根据上次回复线程的日期对线程进行排序的任何想法?
答案 0 :(得分:1)
首先,我强烈建议将last_post_date添加为forum_thread表的列,每次将帖子添加到线程时都会更新。否则,由于加入,您在论坛中获得更多主题时会出现性能问题。
无论如何,回答你的问题:
$thread_sql = "SELECT forum_threads.thread_id AS thread_id,
forum_threads.thread_title AS thread_title,
forum_threads.thread_description AS thread_description,
forum_threads.forum_id AS forum_id,
forum_threads.thread_postdate AS thread_date,
forum_threads.thread_icon AS thread_icon,
forum_threads.thread_views AS thread_views,
forum_threads.user_id AS user_id,
users.user_firstname AS user_firstname,
users.user_lastname AS user_lastname,
max(forum_posts.post_date) last_post_date
FROM forum_threads
INNER JOIN users ON forum_threads.user_id = users.user_id
INNER JOIN forum_posts ON forum_threads.thread_id = forum_posts.thread_id
WHERE forum_threads.forum_id='$_GET[f]'
group by
forum_threads.thread_id,
forum_threads.thread_title,
forum_threads.thread_description,
forum_threads.forum_id,
forum_threads.thread_postdate,
forum_threads.thread_icon,
forum_threads.thread_views,
forum_threads.user_id,
users.user_firstname,
users.user_lastname
ORDER BY max(forum_posts.post_date) DESC ";
此外,我认为这样的事情可能会更快:
SELECT forum_threads.thread_id AS thread_id,
forum_threads.thread_title AS thread_title,
forum_threads.thread_description AS thread_description,
forum_threads.forum_id AS forum_id,
forum_threads.thread_postdate AS thread_date,
forum_threads.thread_icon AS thread_icon,
forum_threads.thread_views AS thread_views,
forum_threads.user_id AS user_id,
users.user_firstname AS user_firstname,
users.user_lastname AS user_lastname
(SELECT MAX(post_date)
FROM forum_posts
WHERE thread_id = forum_threads.thread_id) last_post_date
FROM forum_threads
INNER JOIN users ON forum_threads.user_id = users.user_id
WHERE forum_threads.forum_id='$_GET[f]'
ORDER BY last_post_date DESC;