I've spent over a week trying to see how to prevent duplicate mysql entries into my table. How can I prevent this from happening? I don't want a user to update his/her location if I have an entry from the last 5 seconds, down to the fractional seconds. For instance, I just want the very first entry, and a way to ignore the subsequent entries.
The type of my column is timestamp(6)
| 40 | 4 | 5 | 2016-03-05 22:19:57.572771 |
| 41 | 4 | 5 | 2016-03-05 22:19:57.574012 |
| 42 | 4 | 5 | 2016-03-05 22:19:57.589436 |
| 43 | 4 | 5 | 2016-03-05 22:19:57.598959 |
| 44 | 4 | 5 | 2016-03-05 22:19:57.605516 |
I've tried things like:
"SELECT * FROM checkins WHERE user_id = " + req.body.userID + " AND TIMESTAMPDIFF(SECOND, entered_at, current_timestamp(6)) < 5;"
AND
"SELECT * FROM checkins WHERE user_id = " + req.body.userID + " AND entered_at <= now() AND entered_at >= date_sub(now(), interval 1 SECOND);"
None of which worked unfortunately. Thank you in advance for any guidance.
EDIT
For clarity, what I'm doing is trying to see if a particular user has made an entry within the last 5 seconds, down to the fractional second. If he has, then rows.length > 0
will be true when rows returns on the server side. If in fact rows.length
IS greater than zero, I set my proceedToUpdateUsersLocation
variable to false
. I hope that's a clear explanation. I'm trying to prevent multiple entries by checking that there were no duplicates in the table from within the past X seconds.