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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:56:19+00:00 2026-05-11T14:56:19+00:00

I have asked a similar question before and while the answers I got were

  • 0

I have asked a similar question before and while the answers I got were spectacular I might need to clearify.

Just like This question I want to return N number of rows depending on a value in a column.

My example will be I have a blog where I want to show my posts along with a preview of the comments. The last three comments to be exact.

I have have I need for my posts but I am racking my brain to get the comments right. The comments table has a foreign key of post_id which obviously multiple comments can be attached to one post so if a post has 20 comments then I just want to return the last three. What makes this somewhat tricky is I want to do it in one query and not a ‘limit 3’ query per blog post which makes rendering a page with a lot of posts very query heavy.

SELECT * FROM replies GROUP BY post_id HAVING COUNT( post_id ) <=3 

This query does what I want but only returns one of each comment and not three.

  • 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. 2026-05-11T14:56:20+00:00Added an answer on May 11, 2026 at 2:56 pm
    SELECT  l.* FROM    (         SELECT  post_id,                 COALESCE(                 (                 SELECT  id                 FROM    replies li                 WHERE   li.post_id = dlo.post_id                 ORDER BY                         li.post_id, li.id                 LIMIT 2, 1                 ), CAST(0xFFFFFFFF AS DECIMAL)) AS mid         FROM    (                 SELECT  DISTINCT post_id                 FROM    replies dl                 ) dlo         ) lo, replies l WHERE   l.replies >= lo.replies         AND l.replies <= lo.replies         AND l.id <= lo.mid 

    Having an index on replies (post_id, id) (in this order) will greatly improve this query.

    Note the usage of l.replies >= lo.replies AND l.replies <= lo.replies: this is to make the index to be usable.

    See the article in my blog for details:

    • Advanced row sampling (how to select N rows from a table for each GROUP)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asked a similar question before and got an convincing answer as well?
I had asked a question similar to this before, but I have a new
(Before I start, yes I have asked a similar question before; unfortunately due to
Similar questions to this my have been asked a lot of times before. But
Okay so I know I asked a similar question a while ago, but this
I've asked very similar question before and should have mentioned more detailed. Last time
I believe this question is slightly different than similar ones asked on here before
I have asked a similar question before, but didn't get very good results. I've
This is similar to a question I asked a while ago, however this didn't
I asked a similar question before, but got no helpful response so I will

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.