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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:11:31+00:00 2026-06-14T14:11:31+00:00

I am trying to run a query to select the article that have the

  • 0

I am trying to run a query to select the article that have the most comments related to the article id.

i have a article table and a comments table the comments table has a article_id field that links them and i am trying to do a left join here is want i have so far.

SELECT *, SUM(`comments`.`article_id`) AS total FROM (`articles`) JOIN `comments` ON `comments`.`article_id` = `articles`.`id` GROUP BY `comments`.`article_id` ORDER BY `total` asc

i am using CodeIgniter and the above is the output from my active record which is below.

$this->db->select('*');
        $this->db->from('articles');
        $this->db->join('comments', 'comments.article_id = articles.id');
        $this->db->group_by('comments.article_id');
        $this->db->select_sum('comments.article_id', 'total');
        $this->db->order_by('total', 'asc'); 
        $query = $this->db->get();
        return $query->result();

Ok this works i seem to be getting the correct output but i dont get the number of comments as a value which i need to work from.

so i want to get

article with id 1
has 23 comments

article with id 2
has 3 comments

etc etc

at the moment i am getting a sum of the article ids i think i have a total field with really high values which are not correct can some help with this???

Thanks

SORRY ANSWERED MY OWN QUESTION IM USING SUM AND NOT COUNT AARRRRRGGGGHHHH this works

SELECT *, COUNT(`comments`.`article_id`) AS total FROM (`articles`) JOIN `comments` ON `comments`.`article_id` = `articles`.`id` GROUP BY `comments`.`article_id` ORDER BY `total` asc
  • 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-14T14:11:32+00:00Added an answer on June 14, 2026 at 2:11 pm

    Here is the answer..

    SELECT articles.*, count(comments.article_id) AS total1
    FROM
    articles JOIN comments ON comments.article_id = articles.article_id
    GROUP BY articles.article_id ORDER BY total1 asc

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

Sidebar

Related Questions

I have been trying to run this query to select a row by its
I am trying to run a query that selects values from a table using
So I am trying to run a query that will select, in this case,
I am trying to run a select query that will pull all meds scheduled
I am trying to run the following query: SELECT COUNT(*) FROM ( SELECT *
I'm trying to run this query: SELECT COUNT(events.event_id) AS total_events, COUNT(matches.fight_id) AS total_matches, COUNT(players.fighter_id)
i am trying to run a query like this SELECT a, b , c,
Reg ex again. Trying to run a like SQL query: mysql_query(SELECT * From tablename
I have a very simple query that I am trying to optimize. issues_notes is
I'm trying to run a mysql query which selects all users from a table

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.