Update table and order dates in MySQL


You cannot use UPDATE command with ORDER BY clause, but you can use SELECT statement with ORDER BY DESC.

Let us first create a −

mysql> create table DemoTable1403
   -> (
   -> DueDate timestamp
   -> );
Query OK, 0 rows affected (1.26 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1403 values('2019-09-29');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable1403 values('2016-02-21');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable1403 values('2018-01-31');
Query OK, 1 row affected (0.65 sec)
mysql> insert into DemoTable1403 values('2017-12-01');
Query OK, 1 row affected (0.27 sec)

Display all records from the table using select −

mysql> select * from DemoTable1403;

This will produce the following output −

+---------------------+
| DueDate             |
+---------------------+
| 2019-09-29 00:00:00 |
| 2016-02-21 00:00:00 |
| 2018-01-31 00:00:00 |
| 2017-12-01 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)

Here is the query to order dates with ORDER BY −

mysql> select * from DemoTable1403 order by DueDate DESC;

This will produce the following output −

+---------------------+
| DueDate             |
+---------------------+
| 2019-09-29 00:00:00 |
| 2018-01-31 00:00:00 |
| 2017-12-01 00:00:00 |
| 2016-02-21 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)

Updated on: 11-Nov-2019

288 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements