我有论坛的这个结构(表格)
我想从类别中选择最后一篇文章(来自forum_post表的行)。
到目前为止SQL:
SELECT * FROM table_post
WHERE topic_id = (SELECT MAX(id) FROM table_topic WHERE category_id = {$id})
ORDER BY id ASC LIMIT 1
问题:如何修改此选择以实现我的目标?
答案 0 :(得分:1)
假设"最后"意味着最大的ID,我建议order by
和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;