Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I subtract using SQL in MYSQL between two date time values and retrieve the result in minutes or second?

Tags:

date

sql

mysql

I want to subtract between two date time values using SQL in MySQL such that I get the interval in minutes or seconds. Any ideas? I want to run a SQL query that retrieves uses from a database who have logged in like 10 minutes from the time.

like image 754
Ali Avatar asked Jun 11 '09 14:06

Ali


People also ask

How do I subtract two time values in SQL?

MySQL SUBTIME() Function The SUBTIME() function subtracts time from a time/datetime expression and then returns the new time/datetime.

How do I subtract one date from another in MySQL?

DATE_SUB() function in MySQL is used to subtract a specified time or date interval to a specified date and then returns the date. Parameter: This function accepts two parameters which are illustrated below : date – Specified date to be modified. value addunit – Here the value is date or time interval to subtract.


1 Answers

There are functions TIMEDIFF(expr1,expr2), which returns the value of expr1-expr2, and TIME_TO_SEC(expr3), which expresses expr3 in seconds.

Note that expr1 and expr2 are datetime values, and expr3 is a time value only.

Check this link for more info.

like image 199
Matthew Jones Avatar answered Sep 30 '22 05:09

Matthew Jones