这是我的餐桌安排:
Posts
post_id
post_votes
Comments
comment_id
post_id
comment_time
我无法创建执行以下操作的查询:
如果有必要,我会发布我尝试过的内容。 我只是进入更复杂的查询,非常感谢任何帮助或建议
答案 0 :(得分:2)
下面将通过desc检索10个帖子顺序,也可以通过desc检索5个评论顺序。
select post_id,post_votes,comment_id,comment_time,
@rn := CASE WHEN @prev_post_id = post_id THEN @rn + 1 ELSE 1 END AS rn,
@prev_post_id := post_id from
(select p.post_id,post_votes,comment_id,comment_time from
(SELECT post_id,post_votes from posts order by post_votes desc limit 10) p
left outer join
comments c on p.post_id=c.post_id order by post_id,comment_time desc )m
having rn<=5
SQL FIDDLE HERE(测试通过desc检索3个帖子的样本以及每个帖子的2个评论)。