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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:35:58+00:00 2026-05-25T14:35:58+00:00

Currently I’m running one query to get all posts for a user, then in

  • 0

Currently I’m running one query to get all posts for a user, then in the loop for that, I am querying for the latest 3 comments for that particular post. Super inefficient; I’m querying over and over again for every post.

I would like to consolidate my queries so that I query just once for all posts, and just once for all comments for those particular posts. At the moment I have a comma-separated list that I made for all posts for this user (e.g. “1,5,18,9”)

posts table:

  • posts.id
  • posts.userid

comments table:

  • comments.id
  • comments.relid (this is the postid)
  • comments.userid

The query should use the $posts_list I have, which is the comma-separated list of posts. Or a subselect for all posts for this user, but that seems inefficient since I already have the post list in a string.

Thanks so much for any help!

  • 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-25T14:35:59+00:00Added an answer on May 25, 2026 at 2:35 pm

    Try this query –

    SELECT p.id, p.userid, c.id, c.userid
      FROM posts p
      JOIN (
        SELECT c1.*, COUNT(*) rank FROM comments c1
        LEFT JOIN comments c2
          ON c2.relid = c1.relid AND c2.id <= c1.id
        GROUP BY c1.relid, c1.id
        ) c
      ON p.id = c.relid
    WHERE rank < 4
    

    And add condition you need, i.e. – WHERE p.userid IN (1,5,18,9).

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

Sidebar

Related Questions

Currently I have three models in Django that create a circular reference: A User
Currently, I am developing a product that does fairly intensive calculations using MS SQL
Currently my query is very heavy, the table (table A) I need to update
Currently, most of the popular websites, like google, yahoo detect if the user connection
Currently, my MVC 3 app has a dependency on a static class that is
Currently, I have a log file of messages in one table in a MySQL
Currently, Enum.Parse supports only the comma as the value separator, so that MemberOne,MemberThree will
Currently I have a web service running in a tomcat (http:// localhost:8080/myApp/getUsers ). My
I am currently running into a problem where an element is coming back from
Currently It just opens up the xml, but i want to get a dialog

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.