
- 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
How to order by date and time in MySQL?
You need to use ORDER BY clause for this. Let us first create a table. The query to create a table is as follows
mysql> create table OrderByDateThenTimeDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > ShippingDate date, - > ShippingTime time - > ); Query OK, 0 rows affected (0.56 sec)
Now you can insert some records in the table using insert command. Here, we have two similar dates, but different times i.e. 2018-01-24
mysql> insert into OrderByDateThenTimeDemo(ShippingDate,ShippingTime) values('2018-01-24','16:45:40'); Query OK, 1 row affected (0.13 sec) mysql> insert into OrderByDateThenTimeDemo(ShippingDate,ShippingTime) values('2018-01-24','15:30:35'); Query OK, 1 row affected (0.22 sec) mysql> insert into OrderByDateThenTimeDemo(ShippingDate,ShippingTime) values('2018-04-25','14:20:25'); Query OK, 1 row affected (0.24 sec) mysql> insert into OrderByDateThenTimeDemo(ShippingDate,ShippingTime) values('2019-02-14','18:07:55'); Query OK, 1 row affected (0.13 sec) mysql> insert into OrderByDateThenTimeDemo(ShippingDate,ShippingTime) values('2019-01-31','20:20:30'); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from OrderByDateThenTimeDemo;
The following is the output
+----+--------------+--------------+ | Id | ShippingDate | ShippingTime | +----+--------------+--------------+ | 1 | 2018-01-24 | 16:45:40 | | 2 | 2018-01-24 | 15:30:35 | | 3 | 2018-04-25 | 14:20:25 | | 4 | 2019-02-14 | 18:07:55 | | 5 | 2019-01-31 | 20:20:30 | +----+--------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to order by date and time. The date would be ordered and then the time i.e. the time with similar dates would also be ordered.
mysql> select *from OrderByDateThenTimeDemo order by ShippingDate,ShippingTime;
The following is the output displaying the ordered date and time. For example, for date 2018-01-24, we have two values for time. Using the above query, we have successfully ordered the time as well i.e. 15:30:35 and 16:45:40
+----+--------------+--------------+ | Id | ShippingDate | ShippingTime | +----+--------------+--------------+ | 2 | 2018-01-24 | 15:30:35 | | 1 | 2018-01-24 | 16:45:40 | | 3 | 2018-04-25 | 14:20:25 | | 5 | 2019-01-31 | 20:20:30 | | 4 | 2019-02-14 | 18:07:55 | +----+--------------+--------------+ 5 rows in set (0.00 sec)
- Related Articles
- Sort by date & time in descending order in MySQL?
- Order By date ASC in MySQL?
- MySQL ORDER BY Date field not in date format?
- How to group by date regardless of time in MySQL?
- How to set default date time as system date time in MySQL?
- How to part DATE and TIME from DATETIME in MySQL?
- Order by date set with varchar type in MySQL
- How to Order by date in MySQL but place empty dates in the end?
- How to insert current date/time in MySQL?
- Set current date and time to timestamp in MySQL
- How to ORDER BY RELEVANCE in MySQL?
- How to ORDER BY LIKE in MySQL?
- How to order by timestamp in MySQL?
- How to order by auto_increment in MySQL?
- How to use union and order by clause in MySQL?

Advertisements