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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:03:00+00:00 2026-06-01T18:03:00+00:00

I’ve never used MySQL for anything involving many tables; when I have, I’ve nested

  • 0

I’ve never used MySQL for anything involving many tables; when I have, I’ve nested queries to get the information that I need.

A good example I can think of when I want to output some blog posts and their related comments.

The tables could look like this (simplified to keep the question short):

posts:           id | title | content
comments:   id | post_id | author | content

Where comments.post_id is obviously linked to the relevant post.id.

I’d then output the posts and the comments like this (I’d normally expand the query a little more, but again want to keep the question short):

// Get posts.
$posts = mysql_query("SELECT * FROM posts ORDER BY id DESC");

while($prow = mysql_fetch_assoc($posts))
{
    $pid = $prow["id"];

    // Write post.
    echo '
        <h2 id="post' . $pid . '">' . $prow["title"] . '</h2>
        <p>' . $prow["content"] . '</p>';



    // Get related comments.
    $cquery = mysql_query("SELECT * FROM comments WHERE post_id = '$pid' ORDER BY id DESC");

    while($crow = mysql_fetch_assoc($cquery))
    {
        $cid = $crow["id"];

        // Write comment.
        echo '<p id="comment' . $cid . '"><strong>' . $crow["author"] . ':</strong> ' . $crow["content"] . '</p>';
    }
}

As far as I can tell, this would mean that if I had 10 blog posts, there would be 11 queries (1 for the posts and then an additional query for the comments for each post). That doesn’t seem sane.

I’m sure there’s a way to create a query that will select the posts as well as the relevant comments all-in-one, I just have no idea what the correct way to do that is. I’ve vaguely heard about joins, but when I read about them, they aren’t making much sense to me.

It would be nice to end up with something roughly like:

$query = mysql_query("
    SELECT
        posts.id,
        posts.title,
        posts.content,
        comments.id,
        comments.author,
        comments.content
    FROM
        posts,
        comments
    WHERE
        comments.post_id = post.id,
");

And then I could just do something like this:

while($row = mysql_fetch_assoc($query))
{
    echo '
        <h2 id="post' . $row["posts.id"] . '">' . $row["posts.title"] . '</h2>
        <p>' . $row["posts.content"] . '</p>';

    // Somehow iterate over the comments and display them.
    //
}

I hope I explained what I want to achieve properly.

  • 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-01T18:03:01+00:00Added an answer on June 1, 2026 at 6:03 pm

    I think your way would work (at least it would in MS SQL), but the standard way would be to use a join:

    SELECT
        posts.id AS post_id,
        posts.title,
        posts.content,
        comments.id AS comment_id,
        comments.author,
        comments.content
    FROM posts
    INNER JOIN comments ON comments.post_id = post.id
    ORDER BY post_id, comment_id DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
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
I want to count how many characters a certain string has in PHP, but
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.