- 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
Order MySQL query by multiple ids?
For this, use ORDER BY FIELD(). Let us first create a table −
mysql> create table DemoTable( ClientId varchar(40), ClientName varchar(40) ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('987_John','John'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable values('1000_Sam','Sam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('777_Carol','Carol'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2000_Bob','Bob'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------+ | ClientId | ClientName | +-----------+------------+ | 987_John | John | | 1000_Sam | Sam | | 777_Carol | Carol | | 2000_Bob | Bob | +-----------+------------+ 4 rows in set (0.00 sec)
Following is the query to order MySQL query by multiple ids −
mysql> select *from DemoTable order by field(ClientId,'1000_Sam','2000_Bob','777_Carol','987_John');
This will produce the following output −
+-----------+------------+ | ClientId | ClientName | +-----------+------------+ | 1000_Sam | Sam | | 2000_Bob | Bob | | 777_Carol | Carol | | 987_John | John | +-----------+------------+ 4 rows in set (0.04 sec)
- Related Articles
- MySQL query to order by NULL values
- MySQL query to GROUP BY multiple columns
- Passing Multiple ids to single parameter in MySQL?
- Multiple LIKE Operators with ORDER BY in MySQL?
- How to order MySQL rows by multiple columns?
- MySQL query to select distinct order by id
- Display IDs in a particular order with MySQL IN()?
- MySQL query to order by current day and month?
- Maintain the custom order of the IDs passed in MySQL
- Order by multiple columns not working as expected in MySQL?
- MySQL query to order by two fields and NULL values in chronological order?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Maintaining order in MySQL “IN” query?
- Set different IDs for records with conditions using a single MySQL query
- MySQL select query with multiple WHERE?

Advertisements