php - 我被困在 mysql 中的一个查询上

标签 php mysql

我卡在了 mysql 中的一个查询上。

我想从表中获取最近的评论

  • 评论应该是博客上的最新评论
  • 博客应该是最新的 3 篇博客。
  • 仅在状态为已启用时显示评论和博客

记录应该是这样的
enter image description here enter image description here enter image description here 表的表结构 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

输出


enter image description here

最佳答案

为此,简单的解决方案是对您的结果执行 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/

相关文章:

php - 使用 AFNetworking 快速上传多个图像

php - 这些 PHP if 表达式有什么区别?

javascript - 表单序列化发送数据

mysql - 关于 MySQL 数据库的建议

Python MySQLdb : creating database and filling table

mysql - 使用 VB.Net 连接到在线 MySQL 数据库

mysql存储过程select和update selected

php - Laravel PHP向数据库插入数据时出现无效字符串问题

php - 在 Magento 中从 URL 调整图像大小

PHP mysql使用关键字搜索多个表