- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL ORDER BY 'ENUM' type value based on conditions
For this, use ORDER BY CASE statement. Let us first create a table, wherein we have ENUM type column −
mysql> create table DemoTable1461 -> ( -> DeckOfCards ENUM('K','J','A','Q') -> ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1461 values('K'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1461 values('A'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1461 values('J'); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable1461 values('Q'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1461;
This will produce the following output −
+-------------+ | DeckOfCards | +-------------+ | K | | A | | J | | Q | +-------------+ 4 rows in set (0.00 sec)
Following is the query to order by ENUM type value −
mysql> select * from DemoTable1461 -> order by -> case DeckOfCards when 'A' then 100 -> when 'K' then 101 -> when 'Q' then 102 -> else 103 -> end;
This will produce the following output −
+-------------+ | DeckOfCards | +-------------+ | A | | K | | Q | | J | +-------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to select ENUM('M', 'F') as 'Male' or 'Female'?
- Does SQL Server have an equivalent to MySQL's ENUM data type?
- Resolve the MySQL error 'TYPE=MyISAM'?
- Get the returned record set ordered by (ORDER BY) position in MySQL 'IN' clause
- Simulating MySQL's ORDER BY FIELD() in PostgreSQL?
- How to call another enum value in an enum's constructor using java?
- ORDER BY records in MySQL based on a condition
- Replace records based on conditions in MySQL?
- MySQL SELECT products WHERE 'average price per product' < value?
- How do I change a tag's inner HTML based on their order in JavaScript?
- How to access nested JSON property based on another property's value in JavaScript?
- The difference between 'AND' and '&&' in MySQL?
- Why can't 'kotlin.Result' be used as a return type?
- How to style multi-line conditions in 'if' statements in Python?
- Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?

Advertisements