Php 将另一个数据库联接添加到我的查询会导致某些行不可见
好的,我对这一点并不是特别在行,这实际上是我的第一个连接查询,所以请温柔一点。我将尽可能详细地讲述,因为它可能会像煎锅一样砸到你们大多数人的脸上,但它让我发疯了 我对我试图用codeigniter编写的博客的查询有问题。我已经为帖子设置了一个查询,其中包含两个连接,它的类别使用三个表:posts、categories和posts\u categories。现在我也在尝试连接我的comments表,以进行计数 这是我的模型中的代码,显示了我撰写的两篇一般帖子:Php 将另一个数据库联接添加到我的查询会导致某些行不可见,php,mysql,codeigniter,join,Php,Mysql,Codeigniter,Join,好的,我对这一点并不是特别在行,这实际上是我的第一个连接查询,所以请温柔一点。我将尽可能详细地讲述,因为它可能会像煎锅一样砸到你们大多数人的脸上,但它让我发疯了 我对我试图用codeigniter编写的博客的查询有问题。我已经为帖子设置了一个查询,其中包含两个连接,它的类别使用三个表:posts、categories和posts\u categories。现在我也在尝试连接我的comments表,以进行计数 这是我的模型中的代码,显示了我撰写的两篇一般帖子: $this-
$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你需要使用左外连接,否则你只能得到有评论的帖子。当您执行内部联接(默认)时,它将要求左侧的任何对象在联接的右侧有一个匹配的元素。如果在右侧没有找到匹配项,则忽略它。左外部联接将保持联接左侧的所有元素,而不管右侧是否有匹配项 更改此项:
$this->db->join('comments', 'comments.post_id = posts.id');
到
->join
做什么?如果是内部连接,你的问题可能是它会排除没有任何评论的帖子。你需要在那里使用左外连接,以确保包含没有评论的帖子。通过快速查看你的查询,我假设你的第二篇帖子没有任何评论,并且当你尝试在评论上进行连接时,它不会拾取第二篇帖子。有什么问题吗?哪一行没有看到?如果你的第二篇文章没有评论,它将不会显示。这就是在你的底部结果中发生的事情。我正要打这个。我在评论区向她解释了为什么她的帖子没有出现。是的,已经整理好了,非常感谢大家。告诉过你们这对你们其他人来说是小菜一碟!
$this->db->join('comments', 'comments.post_id = posts.id', 'left outer' );