
- 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
Sort by date & time in descending order in MySQL?
Let us create a table to sort date and time in ascending order. The query to create a table is as follows −
mysql> create table SortByDateAndTime -> ( -> UserId int, -> UserName varchar(100), -> IssueDate date, -> IssueTime time -> ); Query OK, 0 rows affected (0.60 sec)
Insert the records in the table using insert command. The query is as follows −
mysql> insert into SortByDateAndTime values(1,'John','2018-12-16','10:30'); Query OK, 1 row affected (0.14 sec) mysql> insert into SortByDateAndTime values(2,'Bob','2018-12-16','10:10'); Query OK, 1 row affected (0.14 sec) mysql> insert into SortByDateAndTime values(3,'Carol','2018-12-16','10:20'); Query OK, 1 row affected (0.10 sec) mysql> insert into SortByDateAndTime values(4,'Sam','2018-12-16','10:00'); Query OK, 1 row affected (0.15 sec)
The query to display all records from the table using select statement is as follows −
mysql> select *from SortByDateAndTime;
Output
+--------+----------+------------+-----------+ | UserId | UserName | IssueDate | IssueTime | +--------+----------+------------+-----------+ | 1 | John | 2018-12-16 | 10:30:00 | | 2 | Bob | 2018-12-16 | 10:10:00 | | 3 | Carol | 2018-12-16 | 10:20:00 | | 4 | Sam | 2018-12-16 | 10:00:00 | +--------+----------+------------+-----------+ 4 rows in set (0.00 sec)
Here is the query to sort date and time in descending order −
mysql> select UserId,UserName,date(IssueDate) as date1,IssueTime from SortByDateAndTime -> order by date(IssueDate)desc,IssueTime desc;
The following is the output displaying in sorted date and time −
+--------+----------+------------+-----------+ | UserId | UserName | date1 | IssueTime | +--------+----------+------------+-----------+ | 1 | John | 2018-12-16 | 10:30:00 | | 3 | Carol | 2018-12-16 | 10:20:00 | | 2 | Bob | 2018-12-16 | 10:10:00 | | 4 | Sam | 2018-12-16 | 10:00:00 | +--------+----------+------------+-----------+ 4 rows in set (0.00 sec)
Or you can use another query to sort date and time. The query is as follows −
mysql> select UserId,UserName,date(IssueDate) as date1,IssueTime from SortByDateAndTime -> order by date(IssueDate) desc,IssueTime asc;
Output
+--------+----------+------------+-----------+ | UserId | UserName | date1 | IssueTime | +--------+----------+------------+-----------+ | 4 | Sam | 2018-12-16 | 10:00:00 | | 2 | Bob | 2018-12-16 | 10:10:00 | | 3 | Carol | 2018-12-16 | 10:20:00 | | 1 | John | 2018-12-16 | 10:30:00 | +--------+----------+------------+-----------+ 4 rows in set (0.00 sec)
- Related Articles
- How can we sort MySQL output in descending order?
- How to order by date and time in MySQL?
- Sort MongoDB documents in descending order
- Sort a column in descending order after placing argument in MySQL IN()?
- Order By date ASC in MySQL?
- Sort list elements in descending order in C#
- Sort an array in descending order using C#
- How to perform descending order sort in MongoDB?
- Sort Java Vector in Descending Order using Comparator
- MySQL ORDER BY Date field not in date format?
- How to sort TreeSet in descending order in Java?
- Python – Descending Order Sort grouped Pandas dataframe by group size?
- MySQL order by 0 first and then display the record in descending order?
- C# program to sort an array in descending order
- 8085 Program to perform bubble sort in descending order

Advertisements