How to calculate time difference in MYSQL

mysql

Solution

You probably need this if you want to delete records created exactly 2 hours ago:

DELETE FROM TABLE_NAME WHERE created_at = NOW() - INTERVAL 2 HOUR

or this, that will delete all records created more than 2 hours ago:

DELETE FROM TABLE_NAME WHERE created_at < NOW() - INTERVAL 2 HOUR

Problem

I have Rails application which using MYSQL as database. For some condition, I have to delete all the records from table which was stored exactly 2 hours before the current time. My query is : ``` DELETE FROM TABLE_NAME WHERE (NOW() - created_at) > 7200; ``` Here create_at is datetime column type. Storing the value in the format "2012-12-04 06:39:44" My problem is, the above query fetch the records even though the record created time is just 40 to 50 minutes and got deleted. The only problem is the record got delete after it reach 40 to 50 minx from it create time. Can any one please correct my query. I want the MySQL solution. Please help me

Original source