
- 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 difference between dates and calculate salary with MySQL?
Let’s say you need to get the difference between dates (JoiningDate – EndDate) of a month i.e. days to calculate the salary. The daily-wage salary is let’s say 300; therefore for 20 days, it will 6000. In the same way, for 27 days, it will be 8100.
For our example, let us first create a table
mysql> create table DemoTable -> ( -> JoinDate date, -> EndDate date -> , -> Value int -> ); Query OK, 0 rows affected (1.16 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-01','2019-01-31',500); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable values('2019-02-12','2019-02-25',900); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------------+-------------+-------+ | JoinDate | EndDate | Value | +------------+-------------+-------+ | 2019-01-01 | 2019-01-31 | 500 | | 2019-02-12 | 2019-02-25 | 900 | +------------+-------------+-------+ 2 rows in set (0.00 sec)
Following is the query to calculate the salary. For date difference, DATEDIFF() is used −
mysql> select ABS(DATEDIFF(JoinDate,EndDate) * Value) AS Total from DemoTable;
Output
This will produce the following output −
+-------+ | Total | +-------+ | 15000 | | 11700 | +-------+ 2 rows in set (0.14 sec)
- Related Articles
- How to calculate the difference between two dates in JavaScript?
- Find the difference between dates in the form of months with MySQL
- C# Program to get the difference between two dates
- Get the relative timestamp difference between dates in JavaScript
- Calculate the difference between two dates in days, weeks, months and years in Excel
- How to calculate time difference between two times or dates?
- How to get the difference between two dates in Android?
- Not able to get the difference between two dates (SAP)
- C# Program to get the difference between two dates in seconds
- MySQL query to order and display difference between dates from the current date
- Get the SUM of records between two given dates in MySQL
- MySQL to perform DateTime comparison and find the difference between dates in different columns
- Get the time difference between values in different columns with MySQL
- Calculate minutes between two dates in C#
- C Program to calculate the salesmen salary with macro functions.

Advertisements