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

  • Home
  • SEARCH
  • 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 9249403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:10:47+00:00 2026-06-18T10:10:47+00:00

I have a simple articles and comments table. I want to display the articles

  • 0

I have a simple articles and comments table. I want to display the articles along with their comments. I want to union with one select with the comments and another select without the comments on the same table. I have article number 1 with 1 comment and article number 2 with no comments and article number 3 with 2 comments.

The articles table:

articles.id | articles.content
   1        |  test article
   2        |  test another
   3        |   test third

The comments table:

comments.id | comments.aid | comments.comment
  1         |   1          |  bad one
  2         |   3          |  very good
  3         |   3          |   good          

I use the following query to get the results.

SELECT articles.id AS article_id,
comments.id AS comment_id,
comment
FROM articles
LEFT JOIN comments ON comments.aid = articles.id
UNION ALL
SELECT articles.id AS article_id,
NULL,
NULL
FROM articles
GROUP BY article_id
ORDER BY article_id DESC

The result I get which is correct:

article_id | comment_id | comment
   3       | 3          | good
   3       | 2          | very good
   3       | NULL       | NULL
   2       | NULL       | NULL
   2       | NULL       | NULL
   1       | NULL       | NULL
   1       | 1          | bad one

Now if I want to count the comments also I add COUNT to the query and it becomes:

SELECT articles.id AS article_id,
comments.id AS comment_id,
comment ,
COUNT(DISTINCT comments.id) AS count_comments
FROM articles
LEFT JOIN comments ON comments.aid = articles.id
UNION ALL
SELECT articles.id AS article_id,
NULL,
NULL ,
NULL
FROM articles
GROUP BY article_id
ORDER BY article_id DESC

Now the results change after adding the the count column and not all rows are outputted:

article_id | comment_id | comment   | count_comments
  3        | NULL       | NULL      |  NULL
  2        | NULL       | NULL      |  NULL  
  1        | NULL       | NULL      |  NULL
  1        | 1          | bad one   |  3 

Now the comments aren’t displayed except the comment of article 1, ID(2) should be displayed twice for the 2 select commands, and ID(3) should be displayed 3 times (1 for the second select command and 2 for the first select commands as there are 2 comments)

The correct results that I expect:

article_id | comment_id | comment   | count_comments
   3       | 3          | good      |   2 
   3       | 2          | very good |   2
   3       | NULL       | NULL      |   NULL  
   2       | NULL       | NULL      |   NULL
   2       | NULL       | NULL      |   NULL
   1       | NULL       | NULL      |   NULL 
   1       | 1          | bad one   |    1

I don’t know why adding count leads to ths.

Thanks

  • 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-18T10:10:49+00:00Added an answer on June 18, 2026 at 10:10 am

    When you add the count() it affects only the first subquery. So, that subquery is only returning one row rather than multiple rows.

    I’m having problems uploading SQL today, but I think you want something in this form:

    select articles.id AS article_id, comments.id AS comment_id, comment,
           COUNT(DISTINCT comments.id) AS count_comments
    from ((subquery1) union all
          (subquery2)
         ) t
    group by article_id
    order by article_id desc
    

    I assume that you are trying to get all articles included. You don’t need the union all. Your first query is sufficient (because of the left join):

    select articles.id AS article_id, comments.id AS comment_id, comment,
           COUNT(DISTINCT comments.id) AS count_comments
    from articles left join
         LEFT JOIN comments ON comments.aid = articles.id
    group by article_id
    ORDER BY article_id DESC
    

    In the results that you say are correct, you have two rows for article 2, both with NULLs. Is this really what you want? If you want to add that, then put this before the order by:

    union all
    select distinct article_id, NULL, NULL, NULL
    from articles
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple article and comments system with the following tables: Articles table:
I have a really simple Rails app. Basically an article with comments. I want
I have a simple cakephp app with table articles that has a cat_id column
I currently have a very simple MySQL database (articlesDB) with 1 table (articles) and
I have articles table, article_comments. I want to get the value: last_modified_all of article.
I have always seen in python articles/books that python is simple and it has
I have simple table in Sybase -- Creating table 'SimpleText' CREATE TABLE [dbo].[SimpleText] (
I have 3 models: Users, Articles and Comments. Articel has many Comments. Comments belongs
I have a cms which stores comments against articles. These comments can be both
Simple one here but I'm fairly new to Python. I have a string like

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.