Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从数据库中复制条目_Mysql_Codeigniter_Join_Left Join_Duplicate Data - Fatal编程技术网

Mysql 从数据库中复制条目

Mysql 从数据库中复制条目,mysql,codeigniter,join,left-join,duplicate-data,Mysql,Codeigniter,Join,Left Join,Duplicate Data,仔细检查,如果我删除注释表连接,问题就解决了,但显然这不是理想的帮助 我想用Codeigniter写一个博客。目前,我正在显示我想要显示的所有信息,但由于某种原因,在查看博客时,我得到了重复的类别内容。在我的数据库中只有3个测试类别,但是我的测试帖子中有一个显示了6个,而另一个只显示了一个类别两次 更奇怪的是,第一篇博文也算错了评论数。当数据库中只有4条并且只有两条与该帖子相关时,它显示为6条,但最奇怪的是,第二条帖子显示了正确数量的评论,2条。这怎么可能 这是来自相关模型的查询 $this-&

仔细检查,如果我删除注释表连接,问题就解决了,但显然这不是理想的帮助

我想用Codeigniter写一个博客。目前,我正在显示我想要显示的所有信息,但由于某种原因,在查看博客时,我得到了重复的类别内容。在我的数据库中只有3个测试类别,但是我的测试帖子中有一个显示了6个,而另一个只显示了一个类别两次

更奇怪的是,第一篇博文也算错了评论数。当数据库中只有4条并且只有两条与该帖子相关时,它显示为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
可以找到数据库转储

任何帮助都将不胜感激,这让我疯狂


干杯。

运行另一个查询以获取评论计数时,您是否感到不安?假设活动记录,您可以执行以下查询:

$this->db->where'parent\u id',$post\u id; $this->db->count_all_results'comments';
但是,这必须针对每个帖子运行,因此您的查询数量为n+1,其中n=帖子数量。

您是否对数据库手动运行此查询?正如Brendan所说,尝试直接在mysql中运行生成的查询,查看结果,这可以帮助您推断错误,假设您知道sql,您可以通过$this->db->last\u查询获得生成的查询。考虑删除计数,这样你就知道它在计算哪个字段。这很可能是sql错误。也要考虑使用$-> dB->区别;为了避免重复的结果。直接在我的数据库上运行这个,我得到了相同的错误,所以很明显我的查询有问题。我们必须试着重新思考。尽管@Brendan@death\u relic0,我们还是很感激您的创意!这是有问题的查询选择DISTINCT posts.id、posts.title、posts.slug、posts.content、posts.author、posts.date、posts.time、posts.tags、posts.status、GROUP_concatcatcatcategories.name分隔符“-”作为类别,countcomments.id作为来自帖子的总评论左外部加入帖子的类别帖子的类别帖子的类别博客的条目id=帖子的id左加入帖子的类别博客的类别类别id=类别类别类别id=评论的左加入评论的类别id=帖子的组id限制1您可能需要提供sql转储以获得帮助这是因为它非常具体。我不希望对每个帖子都这样做,这是博客滚动的控制器,因此理论上可以在页面上有任意数量的帖子,我不想对每个帖子都运行单独的查询。问题似乎出在注释连接中,如果我删除该连接,那么我会从类别中得到正确的返回,即结果不会加倍,而如果我包含它,即使没有注释的价值计数,我也会根据您提供的SQL转储运行您问题中的查询,只得到2行,但是只有2条带有父id的评论与帖子id相关联。是的,查询应该返回数据库中的两个博客条目,但问题出现在categories和comments列中,这两个都输出了错误的数据。我已经研究了wordpress是如何运行那里的查询的,并且看到他们在posts数据库中保存了一个完整的注释,所以我想我会这样做,我的意思是,如果它对wordpress来说足够好,那么它对我来说可能就足够好了!为你的帮助干杯