
- 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 timestamp in MySQL?
To order by timestamp, use the ORDER BY as in the following syntax −
select *from yourTableName ORDER BY STR_TO_DATE(`yourColumnName`,'%m/%d/%Y%h:%i:%s %p');
Let us first create a table −
mysql> create table DemoTable -> ( -> `timestamp` varchar(100) -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('06/22/2019 01:10:20 PM'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('06/22/2019 12:00:27 PM'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('06/22/2019 06:56:20 AM'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('06/22/2019 07:10:11 AM'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------------------------+ | timestamp | +------------------------+ | 06/22/2019 01:10:20 PM | | 06/22/2019 12:00:27 PM | | 06/22/2019 06:56:20 AM | | 06/22/2019 07:10:11 AM | +------------------------+ 4 rows in set (0.00 sec)
Following is the query to order by timestamp in MySQL −
mysql> select *from DemoTable ORDER BY STR_TO_DATE(`timestamp`,'%m/%d/%Y%h:%i:%s %p');
Output
This will produce the following output −
+------------------------+ | timestamp | +------------------------+ | 06/22/2019 06:56:20 AM | | 06/22/2019 07:10:11 AM | | 06/22/2019 12:00:27 PM | | 06/22/2019 01:10:20 PM | +------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to order by timestamp (descending order) in MongoDB
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- MySQL command to order timestamp values in ascending order?
- MySQL query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?
- How to ORDER BY RELEVANCE in MySQL?
- How to ORDER BY LIKE in MySQL?
- How to order by auto_increment in MySQL?
- How to ORDER BY grouped fields in MySQL?
- How to convert from Unix timestamp to MySQL timestamp value?
- How to sort by value with MySQL ORDER BY?
- How to order by date and time in MySQL?
- How to Order by a specific string in MySQL?
- Convert MySQL timestamp to UNIX Timestamp?
- How to convert timestamp to datetime in MySQL?
- How to order MySQL rows by multiple columns?

Advertisements