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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:21:43+00:00 2026-05-23T01:21:43+00:00

The following query selects all posts and each post’s owner, all of the comments

  • 0

The following query selects all posts and each post’s owner, all of the comments that belong to each post, and the owner of each comment.

I need to only retrieve 5 comments per post. I rewrote the query, but I get an error of “each derived table must have it’s own alias”.

SELECT posts.id AS postId, posts.body, users.id AS userId, users.displayname, comments.id AS commentId, comments.text, commenters.id, commenters.displayname
FROM posts
JOIN users ON posts.owneruserid = users.id
LEFT JOIN comments ON posts.id = comments.postid
    JOIN users AS commenters ON comments.userId = commenters.id
ORDER BY posts.createdAt

New Query:

SELECT posts.id AS postId, posts.body, users.id AS userId, users.displayname
FROM posts
JOIN users ON posts.owneruserid = users.id
LEFT JOIN (
    SELECT comments.id AS commentId, comments.text AS commentText, commenters.id AS commenterId, commenters.displayname AS commenterDisplayName
    FROM comments
    JOIN users AS commenters ON comments.userid = commenters.id
    LIMIT 0,5
        ) AS comments ON comments.postid = posts.id
ORDER BY posts.createdAt

UPDATE The query now works, but it does not produce the desired output. I want to output 10 posts, with 5 comments for each post. This limit clause will only apply for the comments of the first post encountered.

  • 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-23T01:21:44+00:00Added an answer on May 23, 2026 at 1:21 am

    From the edits and comment feedback, here’s the query I think you are looking for… The inner most will prequery gets the posts and who initiated the post, comments and who posted the comments. This inner query is also pre-sorted with the MOST RECENT COMMENTS to the top per postID. Using the result of that, I’m joining to the sql variables (@variables) to get the @varRow increased every time a new comment and reset back to 1 each time a post ID changes (hence the inner PreQuery orders by post ID FIRST). Finally, using the HAVING clause to have the comment’s @varRow count < 6 will get at MOST 5 of each post.

    If you want to limit what posts you are trying to retrieve, I would apply a WHERE clause (such as date/time if available) at the INNER most that generates the “PreQuery”.

    select straight_join
          PreQuery.*,
          @varRow := if( @LastPost = PreQuery.PostID, @varRow +1, 1 ) CommentRow,
          @LastPost := PreQuery.PostID PostID2
       from
          ( select
                  posts.id PostID,
                  posts.body,
                  posts.CreatedAt,
                  u1.id UserID,
                  u1.DisplayName NameOfPoster,
                  c.id,
                  c.userid CommentUserID,
                  c.text CommentText,
                  u2.DisplayName CommentUserName
               from
                  posts
                     join users u1
                        on posts.ownerUserID = u1.id
    
                     LEFT JOIN comments c
                        on posts.id = c.PostID
    
                        join users u2
                           on c.userid = u2.id 
                where
                      posts.id = TheOneParentIDYouWant
                   OR posts.parentid = TheOneParentIDYouWant
                order by
                   posts.ID,
                   c.id desc ) PreQuery,
    
          (select @varRow := 0, @LastPost = 0 ) SQLVars
    
       having
          CommentRow < 6   
    
       order by
          PreQuery.postid,
          CommentRow
    

    — EDIT — per comment
    I THINK what you mean by which “Parent Post” the comments are associated with is because they have the post ID directly. Since the inner-most query does a join of all elements / tables across the board, all are coming along for the ride…

    Post -> User (to get posting user name )
    Post -> Comment (on Common Post ID -- left joined)
            Comment -> User ( to get commenting user name)
    

    Once THAT is all done and sorted by common Post ID and most recent comment sorted to the top, I then apply the @vars against ALL returned rows. The HAVING clause will strip out any comment where it’s sequence is BEYOND the 5 you were looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear all, I have a select query that currently produces the following results: DoctorName
I'm having trouble forming a MySQL query that performs the following action: Select all
I have the following query (all tables are innoDB) INSERT INTO busy_machines(machine) SELECT machine
Sorry if this became a double post. I am writing a query that determines
I'm having an odd problem with the following query, it works all correct, the
I have following query which groups and counts entries for each day : SELECT
The following query: SELECT DISTINCT ClassName FROM SiteTree ORDER BY ClassName is returning things
I have the following query: select column_name, count(column_name) from table group by column_name having
Using the following query: SELECT pe.prodtree_element_name_l, MAX(rs.resource_value) AS resource_value FROM prodtree_element pe LEFT JOIN
I have the following query: SELECT c.* FROM companies AS c JOIN users AS

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.