I have a content feed, ordered by date DESC, id DESC.
date is just a date, not datetime.
id is a typical INT auto_increment.
I need to query for n rows that “come after a specific row”. (imagine an infinite scroll which requires to “fetch items that are older than the current last item”)
I would query for where id < :last_id, but the problem is that the order of date is not coherent with the one of _id.
I would query for where date < :last_date, but the problem is that there are more than 1 items “per day”. I would lose all items that use the same date.
I would query for ORDER BY date DESC, id DESC LIMIT x, n but the feed could change between the 2 queries, adding new items to the top, and therefore shifting the whole offset.
My current hacky solution is to query for where date <= :last_date ORDER BY LIMIT date DESC, id DESC LIMIT n+20 and then “manually” discarding the results that come before the current latest item. (hoping that there aren’t more than 20).
I’m sure I’m not the first one to encounter a similar problem… Any ideas?
If you have more than one item per day, then you should already have a secondary sort order. If it’s id, then write your where clause like this: