
- 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
MySQL command to order timestamp values in ascending order?
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)
- Related Articles
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- MySQL Order by a specific column x and display remaining values in ascending order
- How to order by timestamp in MySQL?
- Order MySQL records randomly and display name in Ascending order
- How to order by timestamp (descending order) in MongoDB
- MySQL query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?
- MySQL ORDER BY with EXPLAIN command
- How can we sort MySQL output in ascending order?
- MySQL query to first set negative value in descending order and then positive value in ascending order
- How to arrange the fractions in ascending order and descending order?
- Select last 20 records ordered in ascending order in MySQL?
- What is ascending order?
- MySQL query to order by two fields and NULL values in chronological order?
- MySQL query to order by NULL values
- How to perform ascending order sort in MongoDB?

Advertisements