
- 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
Maintain the custom order of the IDs passed in MySQL
To maintain the custom order of IDs, use ORDER BY CASE statement. Let us first create a table −
mysql> create table DemoTable1550 -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1550 values(101,'Chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1550 values(110,'Bob'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable1550 values(105,'Carol'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1550 values(109,'Mike'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1550;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 101 | Chris | | 110 | Bob | | 105 | Carol | | 109 | Mike | +------+-------+ 4 rows in set (0.00 sec)
Here is the query to maintain the order of the IDs passed −
mysql> select * from DemoTable1550 -> where Id IN(109,101,110) -> order by case Id -> when 109 then 1001 -> when 101 then 1002 -> when 110 then 1003 -> end;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 109 | Mike | | 101 | Chris | | 110 | Bob | +------+-------+ 3 rows in set (0.00 sec)
- Related Articles
- Order MySQL query by multiple ids?
- Display IDs in a particular order with MySQL IN()?
- Implement Custom Sort Order in MySQL
- Python - Find indexes where values passed as an array should be inserted to maintain order in Pandas index
- MySQL ORDER BY with custom field value
- How to order by a custom rule like order like 4,2,1,3 in MySQL?
- Sort values that contain letters and symbols in custom order with MySQL
- Reserving MySQL auto-incremented IDs?
- MySQL query to ORDER BY `user_id` IN (1,2,3) AND `name` for custom ordering
- Update table with duplicate ids in MySQL
- What are the standard attributes that should be passed to a custom tag in a JSP page?
- Averaging a total from a Score column in MySQL with the count of distinct ids?
- Passing Multiple ids to single parameter in MySQL?
- Count duplicate ids and display the result in a separate column with MySQL
- Get all the IDs of the Time Zone in Java

Advertisements