
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Find the difference between two datetime values with MySQL?
To find the difference between two datetime values, you can use TIMESTAMPDIFF(). Let us first create a table −
mysql> create table DemoTable -> ( -> DueDatetime1 datetime, -> DueDatetime2 datetime -> ); Query OK, 0 rows affected (0.86 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-10-26 19:49:00','2019-10-26 17:49:00'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-10-26 08:00:00','2019-10-26 13:00:00'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-10-26 06:50:00','2019-10-26 12:50:00'); Query OK, 1 row affected (0.68 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+---------------------+ | DueDatetime1 | DueDatetime2 | +---------------------+---------------------+ | 2019-10-26 19:49:00 | 2019-10-26 17:49:00 | | 2019-10-26 08:00:00 | 2019-10-26 13:00:00 | | 2019-10-26 06:50:00 | 2019-10-26 12:50:00 | +---------------------+---------------------+ 3 rows in set (0.00 sec)
Here is the query to implement timestampdiff() and find the difference between two dates −
mysql> select abs(timestampdiff(minute,DueDatetime1,DueDatetime2)) from DemoTable;
This will produce the following output −
+------------------------------------------------------+ | abs(timestampdiff(minute,DueDatetime1,DueDatetime2)) | +------------------------------------------------------+ | 120 | | 300 | | 360 | +------------------------------------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Find the difference between two timestamps in days with MySQL
- C# difference in milliseconds between two DateTime
- MySQL to perform DateTime comparison and find the difference between dates in different columns
- What is the difference between MySQL DATETIME and TIMESTAMP data type?
- How can we calculate the difference between two time values in MySQL?
- Get the time difference between values in different columns with MySQL
- Difference between datetime and datetime-local in HTML5
- How to find absolute difference between two numbers in MySQL?
- Difference between two selects in MySQL?
- Find the difference between dates in the form of months with MySQL
- MySQL difference between two timestamps in Seconds?
- Sorted difference between two columns in MySQL?
- Find the Symmetric difference between two arrays - JavaScript
- Python Pandas – Find the Difference between two Dataframes
- Get a random value between two values in MySQL?

Advertisements