
- 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
MySQL query to calculate the days between two dates from different columns but similar rows
Let us first create a table −
mysql> create table DemoTable1471 -> ( -> EmployeeJoiningDate date, -> EmployeeRelievingDate date -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1471 values('2018-06-21','2018-12-21'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1471 values('2017-01-19','2019-01-31'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1471 values('2015-12-31','2016-03-01'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1471;
This will produce the following output −
+---------------------+-----------------------+ | EmployeeJoiningDate | EmployeeRelievingDate | +---------------------+-----------------------+ | 2018-06-21 | 2018-12-21 | | 2017-01-19 | 2019-01-31 | | 2015-12-31 | 2016-03-01 | +---------------------+-----------------------+ 3 rows in set (0.00 sec)
Here is the query to calculate the days between two dates −
mysql> select datediff(EmployeeRelievingDate,EmployeeJoiningDate) as EmployeeWorkDays from DemoTable1471;
This will produce the following output −
+------------------+ | EmployeeWorkDays | +------------------+ | 183 | | 742 | | 61 | +------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to sum the values of similar columns from two different tables for a particular ID
- How can I calculate full 24hour days between two specified dates in MySQL?
- How to calculate the weeks and days between two dates in Excel?
- How to query between two dates in MySQL?
- Sum columns corresponding values according to similar dates in MySQL?
- How do I calculate number of days between two dates using Python?
- How to calculate the difference between time in different MySQL columns?
- Calculate the difference between two dates in days, weeks, months and years in Excel
- MySQL to perform DateTime comparison and find the difference between dates in different columns
- MySQL query to get next closest day between two days?
- What is the way to find business days between two specified dates in MySQL?
- MySQL query to select all data between range of two dates?
- How to count days between two dates in Java
- Count two different columns in a single query in MySQL?
- MySQL query to find the number of occurrences from two columns?

Advertisements