
- 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 order and display difference between dates from the current date
For this, use ORDER BY clause. The current date is as follows −
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 21:08:16 | +---------------------+ 1 row in set (0.00 sec)
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> DueDate datetime -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(DueDate) values('2019-06-12'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(DueDate) values('2019-06-01'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable(DueDate) values('2019-06-05'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(DueDate) values('2019-06-10'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(DueDate) values('2019-06-11'); Query OK, 1 row affected (0.66 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+---------------------+ | Id | DueDate | +----+---------------------+ | 1 | 2019-06-12 00:00:00 | | 2 | 2019-06-01 00:00:00 | | 3 | 2019-06-05 00:00:00 | | 4 | 2019-06-10 00:00:00 | | 5 | 2019-06-11 00:00:00 | +----+---------------------+ 5 rows in set (0.00 sec)
Here is the query to order by to get the difference and order the dates. The difference between the dates are displayed in a separate column −
mysql> select Id,DueDate,DATEDIFF(DueDate, CURDATE()) AS t from DemoTable -> order by CASE WHEN t < 0 THEN 1 ELSE 0 END, t;
Output
+----+---------------------+------+ | Id | DueDate | t | +----+---------------------+------+ | 4 | 2019-06-10 00:00:00 | 1 | | 5 | 2019-06-11 00:00:00 | 2 | | 1 | 2019-06-12 00:00:00 | 3 | | 2 | 2019-06-01 00:00:00 | -8 | | 3 | 2019-06-05 00:00:00 | -4 | +----+---------------------+------+ 5 rows in set (0.00 sec)
- Related Articles
- Select dates between current date and 3 months from the current date in MySQL?
- MySQL query to get the current date from the list of dates
- MySQL query to retrieve current date from a list of dates
- Find the difference between current date and the date records from a MySQL table
- MySQL DATE function to return the difference between current date and joining date
- How to get the difference between date records and the current date in MySQL?
- MySQL query to get current datetime and only current date
- Display distinct dates in MySQL from a column with date records
- MySQL query to order by current day and month?
- MySQL query to select date >= current date - 3 weeks?
- MySQL query to find the date records wherein the current date and time is in between the JoiningDate and RelievingDate
- MySQL query to get the dates between range of records displaying student’s Date of Birth?
- MySQL time period query to fetch date records from interval of 14 weeks from current date?
- Display Timestamp before the current date in MySQL
- MySQL query to get the current date records wherein one of the columns displays current date

Advertisements