
- 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 ORDER BY with custom field value
To set custom field value, use FIND_IN_SET(). Let us first create a table −
mysql> create table DemoTable677( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserStatus text ); Query OK, 0 rows affected (1.07 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable677(UserStatus) values('BUSY'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable677(UserStatus) values('AT WORK'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable677(UserStatus) values('OFFLINE'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable677(UserStatus) values('BLOCKED'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable677;
This will produce the following output −
+--------+------------+ | UserId | UserStatus | +--------+------------+ | 1 | BUSY | | 2 | AT WORK | | 3 | OFFLINE | | 4 | BLOCKED | +--------+------------+ 4 rows in set (0.00 sec)
Following is the query to ORDER BY with custom field value −
mysql> select *from DemoTable677 ORDER BY FIND_IN_SET(UserStatus, 'BLOCKED,AT WORK,OFFLINE,BUSY');
This will produce the following output −
+--------+------------+ | UserId | UserStatus | +--------+------------+ | 4 | BLOCKED | | 2 | AT WORK | | 3 | OFFLINE | | 1 | BUSY | +--------+------------+ 4 rows in set (0.03 sec)
- Related Articles
- ORDER BY specific field value first in MySQL
- How to perform custom sort by field value in MySQL?
- MySQL IF/WHEN/ELSE/OR with ORDER BY FIELD
- How to sort by value with MySQL ORDER BY?
- MySQL order by field using CASE Statement
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Set a certain value first with MySQL ORDER BY?
- How to order by a custom rule like order like 4,2,1,3 in MySQL?
- MySQL ORDER BY Date field not in date format?
- Simulating MySQL's ORDER BY FIELD() in PostgreSQL?
- Order by a single field and display rest of the records in the same order with MySQL
- How to use ORDER BY field and sort by id in a single MySQL field?
- Implement Custom Sort Order in MySQL
- MySQL Order by with case?
- Selecting a value in custom order from another column in a MySQL table with a single query

Advertisements