将另一个数据库连接添加到我的查询会导致某些行无法看到

时间:2012-03-29 21:25:43

标签: php mysql codeigniter join

对,我根本不熟练,这实际上是我的第一次加入查询,所以要温柔。我会尽可能详细地给出尽可能多的细节,因为它可能会像煎锅那样击中你们大多数人,但是它正在做我的坚果!

我在查询博客时遇到问题我试图在codeigniter中写字。我已经设置了一个包含2个联接的查询,其中包含三个表:帖子,类别和posts_categories现在我也试图加入我的评论表,进行计数。

这是我模型中的代码,显示了我编写的通用帖子:

            $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
                            ');
        $this->db->group_by(array('posts.id'));
        $this->db->from('posts');
        $this->db->join('posts_categories', 'posts_categories.blog_entry_id = posts.id');
        $this->db->join('categories', 'posts_categories.blog_category_id = categories.category_id');
        $query = $this->db->get();
        return $query->result_array();

这是结果:

(
[0] => Array
    (
        [id] => 1
        [title] => My first blog post!
        [slug] => my-first-blog-post
        [content] => This is my first blog post. Don't worry, it's just a test, my real blog won't be this boring, hopefully!
        [author] => Joni
        [date] => 2012-01-23
        [time] => 00:00:00
        [tags] => Testing
        [status] => 
        [categories] => Testing-More Tests-Test
    )

[1] => Array
    (
        [id] => 2
        [title] => This is another test-post
        [slug] => this-is-another-test-post
        [content] => Well you guessed it. another boring test post, enjoy!
        [author] => Joni
        [date] => 2012-01-23
        [time] => 00:00:00
        [tags] => Sexy
        [status] => 
        [categories] => Test
    )

现在,当我修改查询以实现第三次连接时,如下所示:

            $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->from('posts');
        $this->db->join('posts_categories', 'posts_categories.blog_entry_id = posts.id');
        $this->db->join('categories', 'posts_categories.blog_category_id = categories.category_id');
        $this->db->join('comments', 'comments.post_id = posts.id');
        $query = $this->db->get();
        return $query->result_array();

我最终得到了这个

(
[0] => Array
    (
        [id] => 1
        [title] => My first blog post!
        [slug] => my-first-blog-post
        [content] => This is my first blog post. Don't worry, it's just a test, my real blog won't be this boring, hopefully!
        [author] => Joni
        [date] => 2012-01-23
        [time] => 00:00:00
        [tags] => Testing
        [status] => 
        [categories] => Testing-More Tests-Test
        [total_comments] => 3
    )

如果你已经做到这一点,对不起,这太长了,只想提前说谢谢!

欢呼joni

3 个答案:

答案 0 :(得分:4)

您需要使用LEFT OUTER JOIN,否则您只会获得有评论的帖子。当您执行INNER JOIN(默认值)时,它将要求左侧的任何内容在连接的右侧都有匹配的元素。如果它在右侧没有找到匹配项,则省略它。无论右侧是否匹配,LEFT OUTER JOIN都会将所有元素保留在连接的左侧。

改变这个:

$this->db->join('comments', 'comments.post_id = posts.id'); 

$this->db->join('comments', 'comments.post_id = posts.id', 'left outer' );

答案 1 :(得分:1)

->join做什么?如果它正在进行内部联接,那么您的问题可能是它会排除没有任何注释的帖子。您需要在那里使用左外连接,以确保包含没有注释的帖子。

答案 2 :(得分:0)

从快速查看您的查询我假设您的第二篇文章没有任何评论,当您尝试在评论上加入时,它不会收到第二篇文章。