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 8131267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:02:36+00:00 2026-06-06T09:02:36+00:00

I tried different join types but couldn’t figure out which type should I use.

  • 0

I tried different join types but couldn’t figure out which type should I use. I want to count number of comments for each article.

$select = $this->_db->select()
        ->from($this->_name)
        ->joinLeft('categories', 'categories.cat_id = ' . $this->_name . '.category', array('category_name' => 'name'))
        ->joinLeft('comments', 'comments.post_id = ' . $this->_name. '.post_id', array('num_comments' => 'COUNT(*)'))
        ->group($this->_name.'.post_id')
        ->limit(3)
        ->order("pubDate DESC");
if($category_id > 0) $select->where("category = ?", $category_id);
if($last_pub_date > 0) $select->where("$this->_name.pubDate < ?", $last_pub_date);

I use this method also for my Twitter like pagination, so $last_pub_date is for that. With joinLeft() it seems good but of course if there is no comment for an article query fetches count as 1 instead of 0. I tried with other join types but if there is no comment whole row is not fetched or $this->_db->order("pubDate DESC") is not working correctly. I tried also with subquery but I am not sure if I wrote it as it should be.

  • 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-06T09:02:37+00:00Added an answer on June 6, 2026 at 9:02 am

    The left join is right, but you need to use COUNT(comment_id) instead of COUNT(*). COUNT(*) will return the number of rows, which will be one even if there are no comments. COUNT(comment_id) will return the number of non-NULL comment_id values, which should be what you’re looking for.

    http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out the issue, and tried different styles that I
I have searched here on SO and tried different solutions offered here, but cannot
I've searched and searched and tried many different ways, but I can't seem to
I need to join two tables based on columns with different types. The first
I want to know the gallaxies which has planets that have sattelites of type
I have tried all kinds of different ways to query this but just can't
I have tried different methods and none of them seem to work. When I
I've tried different variations of the following statement, the result is half of what
I've tried everything... it won't hide. Obviously tried Mouse.hide() tried different player versions (10.2,
I have a small problem, I have tried different solutions to add a image

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.