我有两个表,帖子和评论。每个帖子至少有一条评论。我的表格如下所示
该post表具有 post idpid和一个title. 该comments表有 post id pid、 comment idcid和 timestampts
table post {
int pid
varchar title
}
table comments {
int pid
int cid
int ts
varchar comment
}
我喜欢通过在顶部显示带有最新评论的帖子来列出所有帖子。
我试过group by了,但没有按预期工作
select p.pid, c.ts from posts p, comments c where c.pid=p.pid group by p.pid order by c.ts desc;
我也试过
select p.pid, c.ts from posts p join (select pid, max(ts) from comments) c on c.pid=p.pid order by c.ts desc;
但它没有返回任何结果。
有人可以帮忙吗