
- 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
Get the time difference between values in different columns with MySQL
For this, you can use time_format() and time_diff(). To find the time difference, you need to use the time_diff() method. Let us first create a table −
mysql> create table DemoTable624 (PunchIn datetime,PunchOut datetime); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable624 values('2019-07-14 12:10:00', '2019-07-14 12:50:00'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable624 values('2019-07-14 11:00:00', '2019-07-14 11:30:00'); Query OK, 1 row affected (0.34 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable624;
This will produce the following output −
+---------------------+---------------------+ | PunchIn | PunchOut | +---------------------+---------------------+ | 2019-07-14 12:10:00 | 2019-07-14 12:50:00 | | 2019-07-14 11:00:00 | 2019-07-14 11:30:00 | +---------------------+---------------------+ 2 rows in set (0.00 sec)
Following is the query to get the time difference −
mysql> select time_format(timediff(PunchIn,PunchOut),'%H hrs, %i min.') from DemoTable624;
This will produce the following output −
+-----------------------------------------------------------+ | time_format(timediff(PunchIn,PunchOut),'%H hrs, %i min.') | +-----------------------------------------------------------+ | -00 hrs, 40 min. | | -00 hrs, 30 min. | +-----------------------------------------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to calculate the difference between time in different MySQL columns?
- How to merge queries in a single MySQL query to get the count of different values in different columns?
- Get all the records with two different values in another column with MySQL
- Count values based on conditions and display the result in different columns with MySQL?
- How can we calculate the difference between two time values in MySQL?
- MySQL to perform DateTime comparison and find the difference between dates in different columns
- How to select different values from same column and display them in different columns with MySQL?
- How to get the difference between two columns in a new column in MySQL?
- Get MySQL maximum value from 3 different columns?
- Get count of zeros for columns values declared with INT type in MySQL
- Find the difference between two datetime values with MySQL?
- How to get the greatest of two columns values in MySQL?
- How to move data between two tables with columns in different MySQL databases?
- Sorted difference between two columns in MySQL?
- How to combine date and time from different MySQL columns to compare with the entire DateTime?

Advertisements