简体   繁体   中英

mysql query for the latest 10 results in a single query

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...?

"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";

LIMIT limits the result set, and SQL_CALC_FOUND_ROWS counts the number of rows that would have been returned without the LIMIT.

SELECT SQL_CALC_FOUND_ROWS *
FROM messages
WHERE thread_id = :thread_id
ORDER BY date_sent DESC
LIMIT 10

Then use mysql_num_row() to get the count.

Let's avoid the loop altogether:

$threadIds = array(100,101,102); // etc

$result = mysql_query("SELECT * FROM messages WHERE thread_id IN (".implode(',', $threadIds).") ORDER BY date_sent DESC LIMIT 10");

That should work. Note that this might be less efficient depending on your data model and the amount of data you're working with.

EDIT

This will give you the OVERALL most recent 10. If you need the most recent 10 for each thread_id , check out RolandoMySQLDBA's answer.

Cheers

Order the dates the other way around?

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

Rolando is right. You must order records backward and after this limit results.

Something like

Select * From 
  (Select top(10) * From Messages Where threadid = ? order by date_sent desc) dummyName
Order By date_sent asc

I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM