dqdl6469
2016-07-28 00:20
浏览 19
已采纳

从类别中选择最新主题(SQL和PHP)

I want to be able to select the latest topic that was posted to a category by selecting it through an SQL query.

I have two tables that I'm going to use.

  • categories
  • topics

A topic row knows what category it belongs to because topics.topic_cat should match with categories.cat_id.

I want to loop through all the categories and show the latest topic posted to them. It looks something like this.

while($row = $result->fetch_assoc()) {
    echo $row['cat_name'] . ' Latest Post is ' . $row['topic_name'];
}

This would be an example of what it would look like if it was executed.

category1 Latest Post is "latest post"
category2 Latest Post is "latest post"
category3 Latest Post is "latest post"

and so on...

What SQL query could I use to select this?

Here are my table structures.

categories

  • cat_id
  • cat_name
  • cat_description

topics

  • topic_id
  • topic_name
  • topic_date
  • topic_cat
  • topic_by
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题