How can get records after specific id in mysql

时光毁灭记忆、已成空白 提交于 2020-01-06 19:57:07

问题


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:

  1. Remember all the IDs you already fetched, and in query provide NOT IN(list of already fetched ids) in WHERE clause, without specifying offset (only LIMIT 2)

  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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!