There is a news feed, how to get the next batch of news (10pcs) starting from the last? The fact is that the issue is not formed in chronological order, i.e. I should get the id of the latest news output on the page and dance from it

And what if the news has been removed (we passed an id that no longer exists)?

  • You can remember the last received news id. Use range () or limit () in the request - Morlok
  • Remember not the last ID, but the maximum value of the key, which is sorted. Yes, and actually what id can be considered "last", if the sorting is not on it goes - Mike
  • When such questions are asked, 1) Show create request. 2) The request that you tried to run! - E_p

1 answer 1

To solve the problem, add sorting by id to the given sorting. In the second request, it is necessary to transfer the values ​​of the id fields and поля сортировки from the last line of the original request. The request will look like this:

  SELECT nl.Id, nl.'поле сортировки', .... FROM 'новостная лента' AS nl WHERE (nl.'поле сортировки' > 'значение поля сортировки из последней строки предыдущего набора') OR ( nl.'поле сортировки' = 'значение поля сортировки из последней строки предыдущего набора' AND nl.Id < 'значение id из последней строки предыдущего набора' ) ORDER BY nl.'поле сортировки' ASC, nl.id DESC LIMIT 10; 

Rextester work example

  • Considering that the data is selected " not in chronological order ", the id is likely to be mixed up and you cannot be guided by it at all - Mike
  • @Mike means you need to navigate not by id , but by the column by which the news is sorted. And further on the client to cut the repeatedly returned lines. UPD see above you said the same thing. - vikolyada
  • one
    But it is not easier to sort by the field by which the issue is made and check for more / less not the ID but the sort key - Mike
  • @Mike is exactly what he meant. - vikolyada
  • @vikolyada unfortunately the column on which sorting is going has sooo many repetitions, it is not unique - Fangog