- 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
Implement Custom Sort Order in MySQL
To implement custom sort order in MySQL, you need to use ORDER BY FIELD(). Let us first create a table −
mysql> create table DemoTable -> ( -> Designation varchar(100) -> ); Query OK, 0 rows affected (1.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Software Engineer'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Associate Software Engineer'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('Software Development Engineer'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Product Manager'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------------------------+ | Designation | +-------------------------------+ | Software Engineer | | Associate Software Engineer | | Software Development Engineer | | Product Manager | +-------------------------------+ 4 rows in set (0.00 sec)
Following is the query to implement custom sort order in MySQL −
mysql> select *from DemoTable -> order by field(Designation,'Associate Software Engineer','Software Engineer','Software Development Engineer','Product Manager');
This will produce the following output −
+-------------------------------+ | Designation | +-------------------------------+ | Associate Software Engineer | | Software Engineer | | Software Development Engineer | | Product Manager | +-------------------------------+ 4 rows in set (0.02 sec)
- Related Articles
- Sort values that contain letters and symbols in custom order with MySQL
- Python program to sort string in custom order
- MySQL ORDER BY with custom field value
- Implement ORDER BY in MySQL to order records in human readable format?
- How to order by a custom rule like order like 4,2,1,3 in MySQL?
- How to perform custom sort by field value in MySQL?
- Call aggregate function in sort order with MySQL
- Maintain the custom order of the IDs passed in MySQL
- Sort by date & time in descending order in MySQL?
- What is the default sort order in MySQL tables?
- How can we sort MySQL output in descending order?
- How can we sort MySQL output in ascending order?
- Custom Sort String in C++
- Implement MySQL ORDER BY without using ASC or DESC?
- How to sort by value with MySQL ORDER BY?

Advertisements