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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:01:35+00:00 2026-05-26T00:01:35+00:00

I’m relatively new to MySQL and I am stumped with this section. I am

  • 0

I’m relatively new to MySQL and I am stumped with this section.

I am building a simple blog page which will display a blog along with the comments associated with it. I currently have two tables to handle this data:

Blogs

.blog_id (primary), .blog_title, .blog_date, .blog_post

Comments

.comment_id (primary), .blog_id, .comment_name, .comment

What I am trying to do is pull a specific blog which matches an ID and pull the comments tied to that blog. Here is my query:

SELECT * FROM blogs JOIN comments ON blogs.blog_id=comments.blog_id WHERE blogs.blog_id = $active ORDER BY comments.comment_id

This query results in pulling the correct information but if there are two comments on the blog, it will display the blog and everything twice as it’s looping through the comments. I want to display all the blog information once and then have it loop through the comments.

I hope I explained this clearly. Any help would be fantastic. Thank you.

  • 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-26T00:01:35+00:00Added an answer on May 26, 2026 at 12:01 am

    The behavior you are describing is what a join is supposed to do. If you only want 1 record for each of the blog entries, then you need to use group by. To get a list of all the blogs records with the number of comments you can do a query like:

    SELECT blogs.*,Count(comments.comment_ID) 
    FROM blogs 
    LEFT JOIN comments 
    ON blogs.blog_id=comments.blog_id WHERE blogs.blog_id = $active 
    ORDER BY comments.comment_id
    GROUP By Blog.BlogID
    

    However, you said you wanted to display just the blog, and then the comments associated with that blog. In that case you would most likely just use an initial query to pull all the blogs, and then another query as you loop through all the blog entries to get the comments for each blog. Here is some pseudocode.

    select * FROM blogs
    
    For each record from above
        Code for displaying blog goes here
    
        Select * FROM Comments where Blog_id = $blogID
    
        For each comment from above query
            code for displaying comment goes here
    
        Next COmment
    
    Next Blog
    

    You could just do a single select for all the comments, which would be more efficient, but makes the resulting code more complex as you have to find the records using php in the resulting list. It’s mush simpler to just use multiple queries, and for most blogs (10 articles on a page, max) it won’t make much of a difference. If you really want to use a single query, you can structure your program as in the pseudocode below.

    SELECT * FROM blogs 
    LEFT JOIN comments ON blogs.blog_id=comments.blog_id 
    WHERE blogs.blog_id = $active ORDER BY blogs.blog_id, comments.comment_id
    
    $LastBlogID = -1
    
    FOR EACH Record
        If $LastBlogID != record['blog_id']
            Display Blog Information
            $LastBlogID = record['blog_id']
        End If
    
        Display Comment Information
    Next Record
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

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.