问题
We have this on mysql:

And want return with 2 limit:
1- "SELECT * FROM table ORDER BY point DESC LIMIT 0,2"
//return 'google' & 'jsfiddle'
2- "SELECT * FROM table ORDER BY point DESC LIMIT 2,2"
//return 'stackoverflow' & 'msn'
3- "INSERT INTO table SET id = 11, name = 'gmail', point = 101 "
4- "SELECT * FROM table ORDER BY point DESC LIMIT 4,2"
//return 'msn' & 'emra'
in number 4 'msn' is duplicate. I want number 4 return 'emra' & 'facebook'.
I can save 'id' and I want query that return records after that id.
[EDIT] let me ask in another way:
we have this records:
id, name, point
4, 'google', 100
6, 'yahoo', 100
3, 'gmail', 100
8, 'ymail', 100
2, 'fb', 100
I have id:3 and want select records that are after this id(in this example 'ymail', 'fb')
this is a ajax load content by scorlling(like facebook) but about max points of users that every moment is changing.
回答1:
If i understand your question correctly (which i doubt), You want a way of fetching additional top results matching your search criteria, after getting first N...
The major problem with this is that the new record (which you show in #3) may go anywhere between the previous results, thus making the records you already fetched obsolete (in order).
So, if you really want this, you should do two things:
Remember all the IDs you already fetched, and in query provide
NOT IN(list of already fetched ids)
inWHERE
clause, without specifying offset (onlyLIMIT 2
)After you fetched that, add the amount of the results you fetched anew to the list and reorder it if needed (some sort of insertion sort)
来源:https://stackoverflow.com/questions/11431279/how-can-get-records-after-specific-id-in-mysql