加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:18:40 所属栏目:PHP教程 来源:网络整理
导读:对,所以,我对此并不是特别熟练,这实际上是我的第一次加入查询,所以要温柔.我将尽可能多地提供细节,因为它可能会像煎锅那样击中你们大多数人,但它正在努力! 我正在尝试用codeigniter编写的博客查询问题.我已经设置了一个带有2个连接的查询用于帖子,它的分类
对,所以,我对此并不是特别熟练,这实际上是我的第一次加入查询,所以要温柔.我将尽可能多地提供细节,因为它可能会像煎锅那样击中你们大多数人,但它正在努力!

我正在尝试用codeigniter编写的博客查询问题.我已经设置了一个带有2个连接的查询用于帖子,它的分类使用了三个表:posts,categories和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,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_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,hopefully!
        [author] => Joni
        [date] => 2012-01-23
        [time] => 00:00:00
        [tags] => Testing
        [status] => 
        [categories] => Testing-More Tests-Test
        [total_comments] => 3
    )

)

如果你已经做到这一点,抱歉这么久了,只是想提前说谢谢!

欢呼乔尼

解决方法

您需要使用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' );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读