Home > Database > Mysql Tutorial > body text

Sort by date and time descending in MySQL?

王林
Release: 2023-09-05 08:21:04
forward
981 people have browsed it

Sort by date and time descending in MySQL?

Let us create a table to sort date and time in ascending order. The query to create the 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)
Copy after login

Use the insert command to insert records into the table. 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)
Copy after login

The query to display all the records in the table using select statement is as follows-

mysql> select *from SortByDateAndTime;
Copy after login

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)
Copy after login

This is the query to sort the date and time in descending order-

mysql> select UserId,UserName,date(IssueDate) as date1,IssueTime from
SortByDateAndTime
   -> order by date(IssueDate)desc,IssueTime desc;
Copy after login

Below is the output shown 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)
Copy after login

Alternatively you can use another query to sort by 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;
Copy after login

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)
Copy after login

The above is the detailed content of Sort by date and time descending in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!