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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:47:03+00:00 2026-06-03T17:47:03+00:00

okay, my problem is a little bit more difficult than the title would tell…

  • 0

okay, my problem is a little bit more difficult than the title would tell…

i have a table with messages, i load all these messages in php using a while loop.

my sql query looks like this at the moment:

"SELECT * 
 FROM messages 
 WHERE thread_id = '" . $thread_id . "'
 ORDER BY date_sent"

works fine and returns all the messages after one another, so that i have the latest message at the bottom (which is important, since my messaging module works like facebook, with a small reply field at the bottom, to let the users chat directly)

now, what i’d like to have is a query that would give me only the latest 10 results in a single query.

i already have the number of wanted results ($number_of_results = 10;)

it’d be really cool, if i didn’t have to count the rows of the result first, but rather would request the result in a single query.

something like:

"SELECT * 
 FROM messages 
 WHERE thread_id = '" . $thread_id . "'
 ORDER BY date_sent 
 LIMIT TOTAL_NUMBER_OF_ROWS_WHERE_THREAD_ID_IS_MY_THREAD_ID-$number_of_results, $number_of_results"

is there a way to do something like this…?

  • 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-03T17:47:05+00:00Added an answer on June 3, 2026 at 5:47 pm
    "SELECT * 
     FROM messages 
     WHERE thread_id = '" . $thread_id . "' 
     ORDER BY date_sent DESC 
     LIMIT 10";
    

    Make sure you have this index

    ALTER TABLE messages 
    ADD INDEX threadid_datesent_ndx( thread_id, date_sent );
    

    If you want the 10 reversed again, then do this:

    "SELECT * 
     FROM
     (
         SELECT * 
         FROM messages 
         WHERE thread_id = '" . $thread_id . "' 
         ORDER BY date_sent DESC LIMIT 10
     ) A
     ORDER BY date_sent";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay i'm a little bit stuck on how to solve this problem. When a
Okay, here's my problem. Please forgive me as it's a little bit complicated. I'm
My problem is a little bit complex. I have the following code: $(document).ready(function() {
Okay, it took me a little while to narrow down this problem, but it
Okay so i have a semi weridish problem with re.sub. Take the following code:
Okay, so here's my problem: I have a list of members on a website,
Okay, so I've got a problem - and I'd love to have it fixed.
I have a little problem with threads in Erlang NIFs. You can view my
i have a little problem about preg_match_all and str_replace <? $source = 'hello @user_name,
I've got a little problem on a goDaddy server. I have a php script

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.