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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:12:49+00:00 2026-05-30T09:12:49+00:00

I have 2 tables one is for articles and second is for comments, now

  • 0

I have 2 tables one is for articles and second is for comments, now I want to get total number of comments and all articles details using 1 query, I dont want to use 2 queries one separately for article details and one for comments, can someone please help me get total number of comments on a specific article and all its comments..

article table… article_id
comments table…. comment_id, comment_article_id

normal to get comments I so
SELECT COUNT(*) as total ….

now I want to get both thing (full articles details from article_id) and from comments table total number of comments (and if possible all comments details on that article ID) with only one query…

regards

  • 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-05-30T09:12:51+00:00Added an answer on May 30, 2026 at 9:12 am

    To get the article details and number of comments you can use the following

    SELECT 
        articles.*,
        COALESCE(COUNT(comments.comment_id),0) AS numberOfCommments
    
    FROM articles
    LEFT JOIN comments
    ON comments.comment_article_id = articles.article_id
    AND comments.comment_type = 'B'
    
    // If selecting a specific article use this line
    WHERE articles.article_id = [[ARTICLE_ID]]
    
    // If selecting all articles with counts use this line
    GROUP BY articles.article_id
    

    You can’t really get the comment details in the same query as the articles table due to the fact that the two will most likely have different fields. If you would really like to just get the article followed by all comments then something like the following would work. With the following, make sure that the columns in the first query match the columns in the second (i.e. the title column from the article matches either an empty string (shown) or a title column from the comments). I would personally not do this, and instead would just do 2 queries, one to get the article details and one to get any comments as that way it doesn’t matter that the column types may not match, and it also means the articles query doesn’t need joining onto comments as you can just count the number of rows that are in the comments query

    Single query union method, not advised

    (
        SELECT
            'article' AS rowType,
            article_date_posted,
            article_title,
            article_content
        FROM articles
        WHERE article_id = [[ARTICLE_ID]]
    )
    UNION
    (
        SELECT
            'comment' AS rowType,
            comment_date_posted,
            '',  // Empty string because comments usually have no title
            comment_content
        FROM comments
        WHERE comment_article_id = [[ARTICLE_ID]]
        AND comment_type = 'B'
        ORDER BY comment_date_posted ASC
    )
    

    2-query method, simpler and in most cases more efficient

    //articles query
    SELECT *
    FROM articles
    WHERE article_id = [[ARTICLE_ID]];
    
    //comments query
    SELECT *
    FROM comments
    WHERE comment_article_id = [[ARTICLE_ID]]
    AND comment_type = 'B';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some tables. These tables all have one column in common called 'classified_id':
I have used a join query for retrieving value from two tables one is
I have two tables. One of events and one of articles. Each event has
I have two tables, one for articles/polls, and one for the votes on these
I have 3 tables: articles, tags and article_tag (the one which connects them). I'd
I have two tables. One is article and the other structure. And the articles
So I have two tables, article and comments (which has one-to-many relationship (1 article
I am working on a project using SqlSiteMapProvider. I have two tables Articles and
I have two tables one has a three column composite key. The other needs
I have 2 tables - one storing user information (id, username, password) and the

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.