Ing. Michal Hudak
Ing. Michal Hudak

Reputation: 5612

Mysql - get last post from category

I have this structure (tables) of forum

tables

I want to select last post (row from forum_post table) from category.

SQL so far:

SELECT * FROM table_post 
WHERE topic_id = (SELECT MAX(id) FROM table_topic WHERE category_id = {$id})       
ORDER BY id ASC LIMIT 1

Question: How to modify this select to achieve my goal?

Upvotes: 0

Views: 595

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269463

Assuming that "last" means the biggest id, I would suggest order by and limit:

select fp.*
from forum_post fp join
     forum_topic ft
     on fp.topic_id = ft.id
where ft.category_id = $id
order by fp.id desc
limit 1;

Upvotes: 1

Related Questions