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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:39:53+00:00 2026-06-17T20:39:53+00:00

I have a simple article and comments system with the following tables: Articles table:

  • 0

I have a simple article and comments system with the following tables:

Articles table:

id | writer | text
 1 | Bob    | first article
 2 | Marley | second article

Comments table:

id | article_id | comment
 1 |      1     |  i love this article
 2 |      1     |  good one
 3 |      2     |  waiting for more

I want to select each article with its comments underneath it. I use the following query:

SELECT * FROM articles LEFT JOIN comments ON articles.id = comments.article_id 

The results I get:

 articles.id   | articles.writer | articles.text  | comments.id | comments.article_id | comments.comment
 1             | Bob             | first article  |    1        |         1           | i love this article
 1             | Bob             | first article  |    2        |         1           | good one  
 2             | Marley          | second article |    3        |         2           | waiting for more   

What I want:

articles.id   | articles.writer | articles.text  | comments.id | comments.article_id | comments.comment
 1            | Bob             | first article  |    1        |         1           | i love this article
NULL          | NULL            |    NULL        |    2        |         1           | good one  
 2            | Marley          | second article |    3        |         2           | waiting for more 

So how do I select each article with its comments and display the article only once not with each comment

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-17T20:39:54+00:00Added an answer on June 17, 2026 at 8:39 pm

    You can use user variables in MySQL to do this:

    SELECT 
      case when rownum =1 then id else null end id,
      case when rownum =1 then writer else null end writer,
      case when rownum =1 then text else null end text,
      comment
    FROM
    (
      SELECT a.id, a.writer, a.text,
        c.article_id, c.comment, 
        @rownum := case
                    when @prev = a.id
                      and @prev_art = c.article_id
                    then @rownum+1 else 1 end rownum,
        @prev := a.id p_id,
        @prev_art := c.article_id p_art
      FROM articles a
      LEFT JOIN comments c
        ON a.id = c.article_id 
      ORDER BY a.id, article_id
    ) src
    

    See SQL Fiddle with Demo.

    The result is:

    |     ID | WRITER |           TEXT |             COMMENT |
    ----------------------------------------------------------
    |      1 |    Bob |  first article | i love this article |
    | (null) | (null) |         (null) |            good one |
    |      2 | Marley | second article |    waiting for more |
    

    Edit, if you need the comment.id, then you can add it to the result:

    SELECT 
      case when rownum =1 then id else null end id,
      case when rownum =1 then writer else null end writer,
      case when rownum =1 then text else null end text,
      commentid,
      article_id,
      comment
    FROM
    (
      SELECT a.id, a.writer, a.text,
        c.article_id, c.comment, c.id commentid,
        @rownum := case
                    when @prev = a.id
                      and @prev_art = c.article_id
                    then @rownum+1 else 1 end rownum,
        @prev := a.id p_id,
        @prev_art := c.article_id p_art
      FROM articles a
      LEFT JOIN comments c
        ON a.id = c.article_id 
      ORDER BY a.id, article_id
    ) src
    

    See SQL Fiddle with Demo. The result is:

    |     ID | WRITER |           TEXT | COMMENTID | ARTICLE_ID |             COMMENT |
    -----------------------------------------------------------------------------------
    |      1 |    Bob |  first article |         1 |          1 | i love this article |
    | (null) | (null) |         (null) |         2 |          1 |            good one |
    |      2 | Marley | second article |         3 |          2 |    waiting for more |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really simple Rails app. Basically an article with comments. I want
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
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.
lets say I have the following tables: 1. comments : c_id , a_id ,
I have simple article application with three tables: article id, title, body, user_id comment
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 made an application recently which has articles, etc, with a commenting/moderation system

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.