mysql查询-博客文章和评论有限制 [英] mysql query - blog posts and comments with limit

查看:94
本文介绍了mysql查询-博客文章和评论有限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表格:评论和帖子.我想使用mysql在每个博客文章条目下显示15个文章的列表和最多2条最新评论.

I have 2 tables: comments & posts. I'd like to display a list of 15 posts and maximum 2 most recent comments under each blog post entry using mysql.

数据库方案如下

posts_table: 
post_id, post_txt, post_timestamp

comments_table: 
post_id, comment_txt, comment_timestamp

mysql查询应如何查找以选择15个帖子及其相关注释(每个帖子最多2个最新注释)?

how should the mysql query look in order to select 15 posts and their related comments (max 2 most recent ones per post)?

谢谢

推荐答案

MySQL LIMIT

SELECT * FROM posts_table LIMIT 0, 15

并拉出最近的评论:

SELECT * FROM comments_table ORDER BY comment_timestamp DESC LIMIT 0, 2

我将留给您以某种方式将两个查询结合在一起...

I'll leave it to you to JOIN the two queries together somehow...

这篇关于mysql查询-博客文章和评论有限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆