MYSQL Join - 父表和子表连接并仅从子表中获取最新记录
我有两个表帖子和评论
帖子表
Post_id
Post_content
评论表
comment_id
Comment
post_id
created_date
一篇帖子可以有多个评论或零个评论
我的要求是使用左外连接获取帖子的最新评论。
我的意思是结果应该是包含以下几列的帖子的一条记录。
post_id,post_content ,comment_id,comment
简而言之,帖子应该与最新评论(如果存在)保持一致。
(目前系统首先获取帖子,然后再次访问服务器以获取要显示的最新评论,考虑一次获取它们,因为我们最初只显示一条评论......不确定什么应该是最好的方法如果想显示多个评论..?)
谢谢您
,问候
Kiran
I have two tables Posts and comments
Post Table
Post_id
Post_content
Comments table
comment_id
Comment
post_id
created_date
One post can have multiple comments or zero comments
My requirement is getting the latest comment for the posts using left outer join .
I mean result should be one record for the post with the below columns .
post_id,post_content ,comment_id,comment
In simple words posts should be getting along with their latest comment if it exists.
( Currently the system is getting the posts first and then going to the server again to get the latest comments to display , thought of getting them in one shot since we are displaying only one comment initially ... Not sure what should be the best approach if wants to display more than one comment ..?)
Thank You
Regards
Kiran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顺便说一句,您应该考虑索引
Comments {post_id,created_date}
以获得最佳性能,但要注意非主键索引开销,以防您使用 InnoDB(请参阅中的“集群的缺点”部分) 本文)。BTW, you should consider indexing
Comments {post_id, created_date}
for optimal performance, but be mindful of the non-primary key index overhead in case you are using InnoDB (see the "Disadvantages of clustering" section in this article).