Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:08:08+00:00 2026-06-01T11:08:08+00:00

Right so, I’m not particularly skilled at this at all, this is in fact

  • 0

Right so, I’m not particularly skilled at this at all, this is in fact my first join query, so be gentle. I’m going to give as much detail as I possible can as it’s probably going to hit most of you in face like a frying pan, but it’s doing my nut in!

I’m having issues with a query for a blog i’m trying to write in codeigniter. I had set up a query with 2 joins for posts and it’s categorys using three tables: posts, categories and posts_categories now i’m trying to join my comments table too, to do a count.

This is the code in my model that displays both the generic posts i’ve made up:

            $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();

this is the result:

(
[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
    )

)

Now when i modify the query to implement the third join for comments like so:

            $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();

I end up with this

(
[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
    )

)

If you’ve made it this far, sorry it’s so long, and just wanna say thanks in advance!

cheers joni

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:08:09+00:00Added an answer on June 1, 2026 at 11:08 am

    You need to be using a LEFT OUTER JOIN otherwise you’ll only get posts that have comments. When you do an INNER JOIN (the default), it will require that whatever is on the left have a matching element on the right side of the join. If it doesn’t find a match on the right side, it omits it. A LEFT OUTER JOIN will keep all of the elements on the left side of the join regardless of whether there is a match on the right side.

    Change this:

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

    to

    $this->db->join('comments', 'comments.post_id = posts.id', 'left outer' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have the following query: User.find(:all, :conditions => [guest = ? AND
Right, I know I am totally going to look an idiot with this one,
Right, this is my first venture into multi threading so could be something incredibly
Right now when I run this it keeps clicking on the same button every
Right now I detect the credit card type based upon the first four numbers
this is what i have right now Drawing an RSS feed into the php,
Right now I'm doing this: class MyTest(Base): __tablename__ = 'mytest' id = Column(Integer, primary_key
Right, this is slightly hard to explain but what I'm after is as follows:
Right now, I do this: try: blah except Exception, e: print e time.sleep(9999) Instead
Right now we have a dll file that contains all the database calls and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.