这些是表格:
threads:
id, date, title, text
comments:
id, thread_id, date, comment
我该如何列出最后一个评论的帖子?
目前看起来如此:
$threads = mysql_query("SELECT id, title FROM threads ORDER BY date ASC");
while ($thread = mysql_fetch_assoc($threads)) {
echo $thread['title'];
}
我无法想象这个人。所以,如果有人能给我一个很棒的手!
干杯!
答案 0 :(得分:5)
试试这个:
SELECT DISTINCT t.id, t.title
FROM threads t LEFT JOIN comments c ON t.id = c.thread_id
ORDER BY c.date DESC
如果线程没有注释,则需要左连接。
答案 1 :(得分:1)
这个应该完成它:
SELECT threads.id, threads.title, max(comments.date) FROM threads LEFT OUTER JOIN comments ON threads.id = comments.thread_id GROUP BY threads.id, threads.title ORDER BY max(comments.date) DESC
答案 2 :(得分:0)
select * from Threads t
inner join Comments c
on t.id = c.Thread_id
order by c.date
答案 3 :(得分:0)
由于您需要一个包含最新评论的主题列表,您必须按ID加入,然后按评论日期对DESC进行排序。
SELECT t.id, t.title, c.date
FROM threads t, comments c
WHERE t.id = c.thread_id
ORDER BY c.date DESC
由于