
- 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 query to sort by both timestamp and enum?
For this, you can use ORDER BY DATE(). Let us first create a table. Here, we have a column with type DATE and another with type ENUM −
mysql> create table DemoTable -> ( -> JoiningDate date, -> Status ENUM('Good','Excellent','Bad') -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-21','Excellent'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Bad'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Good'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+-----------+ | JoiningDate | Status | +-------------+-----------+ | 2019-01-21 | Excellent | | NULL | Bad | | NULL | Good | +-------------+-----------+ 3 rows in set (0.00 sec)
Here is the query to sort order by both timestamp and enum −
mysql> select *from DemoTable -> order by DATE(JoiningDate) ASC, Status asc;
This will produce the following output −
+-------------+-----------+ | JoiningDate | Status | +-------------+-----------+ | NULL | Good | | NULL | Bad | | 2019-01-21 | Excellent | +-------------+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to convert timestamp to month?
- MySQL query to sort by certain last string character?
- MySQL query to select date from timestamp?
- MongoDB query to sort by words
- How to order by timestamp in MySQL?
- How to select part of a Timestamp in a MySQL Query?
- MySQL query to fetch records wherein timestamp is before 15+ days?
- MySQL query to sort multiple columns together in a single query
- Convert MySQL timestamp to UNIX Timestamp?
- Count and sort rows with a single MySQL query
- MySQL query to sum up values of rows and sort the result?
- MySQL query to perform sort order on same field
- MySQL query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?
- MySQL query to order by current day and month?
- How to concatenate strings using both GROUP_CONCAT() and CONCAT() in the same MySQL query?

Advertisements