如果我删除评论表连接,仔细检查问题就解决了,但显然这不是理想的帮助!
我正在尝试用 Codeigniter 写博客。目前我正在显示我想要显示的所有信息,但由于某种原因,在查看博客时,我有重复的类别内容。我的数据库中只有 3 个测试类别,但我的测试帖子中有 1 个选择了所有 3 个,其中显示了 6 个,而另一个只显示了一个类别两次。
比这更奇怪的是,第一篇博文还计算了错误的评论数量。当数据库中只有 4 个并且只有两个与该帖子相关时,它显示 6,但最奇怪的是第二个帖子显示了正确数量的评论,两个。这个怎么可能。
这是来自相关模型的查询。
$this->db->select('posts.id,
posts.title,
posts.slug,
posts.content,
posts.author,
posts.date,
posts.time,
posts.tags,
posts.status,
GROUP_CONCAT(categories.name SEPARATOR \'-\') AS categories,
count(comments.id) as total_comments
');
$this->db->group_by(array('posts.id'));
$this->db->join('posts_categories', 'posts_categories.blog_entry_id = posts.id', 'left outer', 'left outer');
$this->db->join('categories', 'posts_categories.blog_category_id = categories.category_id', 'left outer');
$this->db->join('comments', 'comments.post_id = posts.id', 'left outer' );
$query = $this->db->get('posts', $config['per_page'], $this->uri->segment(3));
生成的标准查询是这样的:
SELECT DISTINCT `posts`.`id`,
`posts`.`title`, `posts`.`slug`,
`posts`.`content`, `posts`.`author`,
`posts`.`date`, `posts`.`time`,
`posts`.`tags`, `posts`.`status`,
GROUP_CONCAT(categories.name SEPARATOR '-') AS categories,
count(comments.id) as total_comments
FROM (`posts`)
LEFT OUTER JOIN `posts_categories` ON `posts_categories`.`blog_entry_id` = `posts`.`id`
LEFT JOIN `categories` ON `posts_categories`.`blog_category_id` = `categories`.`category_id`
LEFT JOIN `comments` ON `comments`.`post_id` = `posts`.`id`
GROUP BY `posts`.`id` LIMIT 1
可以在此处找到数据库转储
任何帮助将不胜感激,这让我发疯了!
干杯。