我被困在mysql中的一个查询中。
我想从表中获取最新评论
记录应该是这样的 表格博客
的表格结构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')
表 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')
下面是我写的查询,但我得到的结果是错误的。
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
输出
答案 0 :(得分:1)
SELECT b.blog_title,b.blog_image, bc.*
FROM blog b
left join (
Select * from
blog_comments bc
WHERE bc.comment_status='Enable'
GROUP BY bc.fk_blog_id
having max(bc.comment_date) = bc.comment_date
) bcc on b.blog_id=bcc.fk_blog_id
where
b.blog_status='Enable'
ORDER BY b.blog_create_date desc
LIMIT 0,3
试试这个
答案 1 :(得分:1)
试试这个:
SELECT * FROM blog_comments bc, blog b
WHERE `bc.comment_status`='Enable'
AND `b.blog_status`='Enable'
AND `b.blog_id`=bc.fk_blog_id
ORDER BY `bc.comment_date` DESC LIMIT 1;
尝试更简单的一个:
SELECT * FROM `blog_comment` WHERE 'blog_status'='Enable' AND 'blog_id'='$blogidherefromtitle' ORDER BY 'comment_date' DESC LIMIT1
答案 2 :(得分:1)
为此,简单的解决方案将执行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;
}
}
答案 3 :(得分:0)
试
SELECT b.blog_title,b.blog_image, bc.*
FROM blog_comments AS bc, blog AS 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;
(我不是100%肯定)
答案 4 :(得分:0)
SELECT b.blog_title,b.blog_image, bc.*
FROM blog_comments bc JOIN blog b ON bc.fk_blog_id = b.blog_id
WHERE bc.comment_status='Enable'
AND b.blog_status='Enable'
GROUP BY bc.fk_blog_id
ORDER BY bc.comment_date DESC
LIMIT 0,3
答案 5 :(得分:0)
尝试此查询
SELECT
bc.*
FROM
blog AS b
INNER JOIN (SELECT id , MAX(id) AS MaxID FROM blog) AS bl ON bl.id = b.id
LEFT JOIN blog_comment AS bc ON bc.fk_blog_id = b.id
ORDER BY bc.comment_id DESC
LIMIT 3
编辑:
SELECT
bc.*
FROM
blog AS b
INNER JOIN (SELECT id , MAX(id) AS MaxID FROM blog GROUP BY id) AS bl ON bl.id = b.id
INNER JOIN (SELECT MAX(id) , fk_blog_id FROM blog_comment GROUP BY id) AS bc ON bc.fk_blog_id = b.id
ORDER BY bc.comment_id DESC
LIMIT 3
这是针对每个博客的3个最新博客和最新单个评论
此处使用内部联接将获取最新的博客。而不是加入评论并按日期或身份对其进行排序,并根据您的要求对其进行限制。
答案 6 :(得分:0)
select b.blog_title, b.blog_image, bc.*
from blog b join
(select bc.*
from bc join (select fk_blog_id, max(comment_date) latest_date
from blog_comment
where comment_status = 'Enable'
group by fk_blog_id) latest
on bc.fk_blog_id = latest.fk_blog_id and bc.comment_date = latest_date) c
on b.blog_id = c.fk_blog_id
where b.blog_status = 'Enable'
order by c.comment_date desc
limit 0, 3
c
子查询使用链接问题中的技术查找每个博客的最新评论行。然后将其与blog
表联接以获取相应的博客数据。