Prevent duplicated data when paginated through records that has the same value in MySQL
1
vote
1
answer
614
views
I have a MySQL database for example,
| ID | Title | Purchase_At
| -------- | -------------- | ------ |
| 1 | Title A | 2023-12-01
| 2 | Title B | 2023-08-22
| 3 | Title C | 2023-12-01
| 4 | Title D | 2023-08-23
| 5 | Title E | 2023-12-01
| 6 | Title F | 2023-06-22
| 7 | Title G | 2023-12-01
| 8 | Title H | 2023-08-02
I'm building infinite loading in both direction.
Say my initial retrieval is
SELECT * FROM table ORDER BY Purchase_At DESC LIMIT 3
and it returns ID 1, 3 and 5.
If i want to load anything before ID 1, I do SELECT * FROM table WHERE Purchase_At '2023-12-01' ORDER BY Purchase_At DESC LIMIT 3
?
As you can see, there are chance that i might encounter repeated data since the ordering on multiple records with same value isn't reliable. I can't do WHERE ID 5
either.
I can't use paignation query like LIMIT
or OFFSET
either because during user viewing, new records will be added and that'll mess up the pagination.
Purchase_At is something user entered via a datepicker for receipt date and that's it, there is no millisecond etc... Year, Month and Day is all I got. In my case, there could be user with 10 receipts in same day. If I were to paginate it 3 per page, how can i make sure my infinite loading doesn't produce repeated data?
---
To make my question easier to understand, think as of a fb chat system. Where user scroll down to load new messages and scroll up to load old messages. How can i achieve that same by only knowing my sort can have multiple same Purchase_At value with just year, month and day.
Asked by Gummi
(13 rep)
Jan 14, 2024, 04:27 AM
Last activity: Jan 15, 2024, 11:56 PM
Last activity: Jan 15, 2024, 11:56 PM