
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Order Timestamp Values in Ascending Order using MySQL Command
You can use ORDER BY ASC to order timestamp values in ascending order with TIMESTAMP() method.
The following is the syntax using TIMESTAMP() −
SELECT timestamp( yourTimestampColumnName ) as anyAliasName From yourTableName order by 1 ASC
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table Timestamp_TableDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> yourTimestamp timestamp -> ); Query OK, 0 rows affected (0.83 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into Timestamp_TableDemo(yourTimestamp) values('2019-02-06 17:34:57'); Query OK, 1 row affected (0.14 sec) mysql> insert into Timestamp_TableDemo(yourTimestamp) values('2019-02-06 17:32:30'); Query OK, 1 row affected (0.25 sec) mysql> insert into Timestamp_TableDemo(yourTimestamp) values('2019-02-06 17:32:09'); Query OK, 1 row affected (0.14 sec)
Now you can display all records from the table using a select statement. The query is as follows −
mysql> select *from Timestamp_TableDemo;
The following is the output −
+----+---------------------+ | Id | yourTimestamp | +----+---------------------+ | 1 | 2019-02-06 17:34:57 | | 2 | 2019-02-06 17:32:30 | | 3 | 2019-02-06 17:32:09 | +----+---------------------+ 3 rows in set (0.00 sec)
Here is the query to order timestamp values in ascending order using TIMESTAMP() method −
mysql> SELECT timestamp( yourTimestamp) as 'timestampDemo' -> from Timestamp_TableDemo -> order by 1 asc;
The following is the output −
+---------------------+ | timestampDemo | +---------------------+ | 2019-02-06 17:32:09 | | 2019-02-06 17:32:30 | | 2019-02-06 17:34:57 | +---------------------+ 3 rows in set (0.00 sec)
Advertisements