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