詳細に調べると、コメントテーブルの結合を削除すると問題は解決しますが、これは理想的なヘルプではありません。
Codeigniterでブログを書こうとしています。現時点では、表示したいすべての情報を表示していますが、何らかの理由でブログを表示しているときに、カテゴリのコンテンツが重複しています。データベースには3つのテストカテゴリしかありませんが、3つすべてが選択されているテスト投稿の1つは6つを表示し、もう1つは1つのカテゴリだけを2回表示しています。
それよりもさらに奇妙なことに、最初のブログ投稿も間違った数のコメントを数えています。データベースに4つしかなく、その投稿に関連するのは2つだけの場合、6と表示されますが、最も奇妙なのは、2番目の投稿に正しい数のコメントが表示されることです。どうしてそうなるのでしょう。
これは、問題のモデルからのクエリです。
$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
データベースダンプはここにあります
どんな助けでも大歓迎です、これは私を夢中にさせています!
乾杯。