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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:14:04+00:00 2026-06-02T05:14:04+00:00

I know this question has been asked multiple times (however, I could still not

  • 0

I know this question has been asked multiple times (however, I could still not find a solution):

  • PHP MYSQL showing posts with comments
  • mysql query – blog posts and comments with limit
  • mysql structure for posts and comments
    …

Basic question: having tables posts, comments, user… can you with one single select statement select and show all posts and all comments (with comment.user, comment.text, comment.timestamp)? How would such a select statement look like? If not, what is the easiest solution?

I also tried to JOIN the comments table with the posts table and use GROUP BY, but I got either only one comment in each row or each comment but also those posts multiple times!?

I tried the solution of the first link (nested mysql_query and then fetch) as well as the second link (with arrays). However, the first caused a bunch of errors (the syntax in that post seems to be not correct and I could not figure out how to solve it) and in the second I had problems with the arrays.

My query looks like this till now:

SELECT p.id, p.title, p.text, u.username, c.country_name, (SELECT SUM(vote_type) FROM votes v WHERE v.post_id = p.id) AS sum_vote_type FROM posts p LEFT JOIN user u ON ( p.user_id = u.id ) LEFT JOIN countries c ON ( c.country_id = u.country_id ) ORDER BY $orderby DESC

I was wondering if this issue was not very common, having posts and comments to show…?

Thank you for every help in advance!

  • 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-02T05:14:06+00:00Added an answer on June 2, 2026 at 5:14 am

    Not knowing your database structure, it should look something like this. Note that you should replace the * characters with more explicit lists of columns you actually need.

    SELECT p.*, c.*, u.* FROM posts p
    LEFT JOIN comments c ON c.post_id = p.id
    LEFT JOIN users u ON u.id = p.author_id
    

    Note that if you’re just trying to get counts, sums and things like that it’s a good idea to cache some of that information. For instance, you may want to cache the comment count in the post table instead of counting them every query. Only count and update the comment count when adding/removing a comment.

    EDIT:
    Realized that you also wanted to attach user data to each comment. You can JOIN the same table more than once but it gets ugly. This could turn into a really expensive query. I also am including an example of how to alias columns so it’s less confusing:

    SELECT p.*, c.*, u.name as post_author, u2.name as comment_author FROM posts p
    LEFT JOIN comments c ON c.post_id = p.id
    LEFT JOIN users u ON u.id = p.author_id
    LEFT JOIN users u2 ON u2.id = c.author_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked quite a few times, however, I have
I know this question has been asked multiple times but all the answers seem
I know this flavor of question has been asked multiple times, but I've spent
I know this question has been asked lots of times, but I am not
I know this question has been asked multiple number of times and i have
I know this question has been asked multiple times on this site in one
I know this question has been asked multiple times, but nobody has been able
I know this question has been asked several times, however, after digging through question
I know this question has been asked numerous number of times, both here and
I know this question has been asked several times, but I can't quite seem

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.