Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if mySQL record added in the last x seconds

Tags:

mysql

I have a mySQL database and a table where new records for a project are created. Each project created has a "project name" and an event created date (of type DATETIME).

There can be two projects created with the same name, but if they get created by the same user in quick succession, it is safe to assume it was a mistake on the user's part (clicking twice, refreshing the browser when event variables are passed, etc.).

How do I write a SQL statement to check if a record with the same name already exists, it was added in the last 10 seconds? So far I have the following, although I don't know how to check for the last 10 seconds.

select * from projects where user = 'johnsmith' AND projectname = 'test' AND active='y' AND DATE(projectcreatedon) = CURRENT_DATE AND DATEPART() < ....?
like image 448
zee Avatar asked Jun 19 '11 05:06

zee


People also ask

How can I tell when a MySQL table was last updated?

SELECT UPDATE_TIME FROM information_schema. tables WHERE TABLE_SCHEMA = 'yourDatabaseName' AND TABLE_NAME = 'yourTableName'; Let us implement the following query to get the last updated time. mysql> SELECT UPDATE_TIME -> FROM information_schema.

How do I find the last updated column in MySQL?

USE GEEKSFORGEEKS; Output: To have the latest updated records, we should have a column such as “last updated” with the “Timestamp” column in any table and when a record is newly inserted, it should get the current timestamp value for that column.

Which query can be used to find the last record entry?

We can use the ORDER BY statement and LIMT clause to extract the last data. The basic idea is to sort the sort the table in descending order and then we will limit the number of rows to 1. In this way, we will get the output as the last row of the table. And then we can select the entry which we want to retrieve.

How do I get the second last row in MySQL?

Here is the query to get the second last row of a table in MySQL. mysql> select *from secondLastDemo order by StudentId DESC LIMIT 1,1; The output displays the second last record.


3 Answers

replace AND DATE(projectcreatedon) = CURRENT_DATE AND DATEPART() < ....? with:

AND projectcreatedon > (now() - INTERVAL 10 SECOND)
like image 76
Gryphius Avatar answered Oct 21 '22 05:10

Gryphius


I would suggest not to keep such checks in MySQL because that might not be the perfect way of knowing mistakes because the user might well click the submit or refresh the page after 10 seconds. Instead, put checks in the front-end code to disable clicking the submit button twice or redirect the user to a page where no variables are passed.

But if that isn't what you would like to do, then this might be your query:

SELECT *
FROM `projects`
WHERE `user` = 'johnsmith'
AND `projectname` = 'test' 
AND `active`='y' 
AND TIMESTAMPDIFF(SECOND, projectcreatedon, now()) > 10;
like image 21
Abhay Avatar answered Oct 21 '22 05:10

Abhay


You're trying to fix the problem in the wrong way. Why not eliminate the problem at the source? Make it impossible for the user to create these two projects successively.

If your app makes it possible for a user to submit a form multiple times via refresh, consider using a redirect after the GET/POST variables have been processed.

Furthermore, use simple client-side tricks to disable the submit button after it has been clicked once. You can accomplish this with a very small amount of jQuery

like image 24
Jordan Arseno Avatar answered Oct 21 '22 05:10

Jordan Arseno