
- 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 ORDER BY `user_id` IN (1,2,3) AND `name` for custom ordering
To implement IN() for custom ordering, use ORDER BY CASE.
Let us first create a table −
mysql> create table DemoTable752 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(100) ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable752(Name) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable752(Name) values('Carol'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable752(Name) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable752(Name) values('Mike'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable752(Name) values('Sam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable752(Name) values('Adam'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable752(Name) values('Chris'); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable752;
This will produce the following output -
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Mike | | 5 | Sam | | 6 | Adam | | 7 | Chris | +----+-------+ 7 rows in set (0.00 sec)
Following is the query to order by with custom values −
mysql> select *from DemoTable752 order by case when Id IN (1,2,3) then 1 else Id END, Name;
This will produce the following output -
+----+-------+ | Id | Name | +----+-------+ | 3 | Bob | | 2 | Carol | | 1 | John | | 4 | Mike | | 5 | Sam | | 6 | Adam | | 7 | Chris | +----+-------+ 7 rows in set (0.00 sec)
- Related Articles
- MySQL ORDER BY with different ordering for some of the values as descending and others ascending?
- MySQL query to order by two fields and NULL values in chronological order?
- MySQL query to order by current day and month?
- MySQL ORDER BY with custom field value
- MySQL query to order by NULL values
- How to order by a custom rule like order like 4,2,1,3 in MySQL?
- MySQL query to display custom text for empty columns
- Order MySQL query by multiple ids?
- MySQL query to select distinct order by id
- Implement Custom Sort Order in MySQL
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Ordering alphabetically in MySQL except for one entry?
- MySQL Order By date column and integer column, but specify ordering rules of integer column? Is it possible?
- Using CASE statement in MySQL to display custom name for empty value
- How to order and select query with conditions in MySQL?

Advertisements