我应该如何加入这些表?

2 mysql sql join

这些是表格:

threads:
id, date, title, text


comments:
id, thread_id, date, comment
Run Code Online (Sandbox Code Playgroud)

我该如何列出最后一个评论的帖子?

目前它的外观如下:

$threads = mysql_query("SELECT id, title FROM threads ORDER BY date ASC");

while ($thread = mysql_fetch_assoc($threads)) {

 echo $thread['title'];

}
Run Code Online (Sandbox Code Playgroud)

我无法想象这个人.所以,如果有人能给我一个很棒的牌!

干杯!

Chs*_*y76 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
Run Code Online (Sandbox Code Playgroud)

如果您的线程没有注释,则需要左连接.