我卡在了 mysql 中的一个查询上。
我想从表中获取最近的评论
- 评论应该是博客上的最新评论
- 博客应该是最新的 3 篇博客。
- 仅在状态为已启用时显示评论和博客
记录应该是这样的
表的表结构 blog
blog_id int - primary (auto increment) blog_title -varchar blog_desc -varchar blog_image -varchar blog_tags -varchar tot_comments -int blog_creater -varchar blog_create_date -datetime blog_status -enum ('Enable','Disable')
table structure for the table blog_comment
comment_id -int (auto increment) fk_blog_id -int comment -varchar comment_by -varchar email -varchar comment_date -datetime comment_status -enum ('Enable','Disable')
And below is query written by me, but the result I am getting is wrong.
SELECT b.blog_title,b.blog_image, bc.*
FROM blog_comments bc, blog b
WHERE bc.comment_status='Enable'
AND b.blog_status='Enable'
AND b.blog_id=bc.fk_blog_id
GROUP BY bc.fk_blog_id
ORDER BY bc.comment_date DESC
LIMIT 0,3
输出
最佳答案
为此,简单的解决方案是对您的结果执行 2 个查询。首先查询获取博文结果
$db_blog="select blog_id,blog_title from blog where blog_ststus='Enable'";
$que=mysql_query($db_blog);
while($row=mysql_fetch_object($que))
{
echo $row->blog_title;
$db_comment="select comment from blog_comments where fk_blog_id=".$row->blog_id." and comment_status='Enable' order by comment_date desc";
$quec=mysql_query($db_comment);
while($comment=mysql_fetch_object($quec))
{
echo $comment->comment;
}
}
关于php - 我被困在 mysql 中的一个查询上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14749107/