- 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
Get the returned record set ordered by (ORDER BY) position in MySQL 'IN' clause
To return record set order, you need to use FIND_IN_SET(). Let us first create a table −
mysql> create table recordSetOrderDemo -> ( -> EmployeeId int, -> EmployeeName varchar(30) -> ); Query OK, 0 rows affected (0.63 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into recordSetOrderDemo values(20,"John"); Query OK, 1 row affected (0.20 sec) mysql> insert into recordSetOrderDemo values(10,"Larry"); Query OK, 1 row affected (0.14 sec) mysql> insert into recordSetOrderDemo values(100,"Mike"); Query OK, 1 row affected (0.14 sec) mysql> insert into recordSetOrderDemo values(50,"Sam"); Query OK, 1 row affected (0.11 sec) mysql> insert into recordSetOrderDemo values(10,"David"); Query OK, 1 row affected (0.18 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from recordSetOrderDemo;
This will produce the following output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 20 | John | | 10 | Larry | | 100 | Mike | | 50 | Sam | | 10 | David | +------------+--------------+ 5 rows in set (0.00 sec)
Following is the query to return record set order −
mysql> select *from recordSetOrderDemo where EmployeeId IN(100,10,20,50) -> ORDER BY FIND_IN_SET(EmployeeId,'100,10,20,50');
This will produce the following output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 100 | Mike | | 10 | Larry | | 10 | David | | 20 | John | | 50 | Sam | +------------+--------------+ 5 rows in set (0.03 sec)
- Related Articles
- Get the returned record set order in MySQL IN clause?
- MySQL ORDER BY 'ENUM' type value based on conditions
- Simulating MySQL's ORDER BY FIELD() in PostgreSQL?
- Order by selected record in MySQL?
- What is the importance of the order of Columns in the SET clause of UPDATE statement? Will it make big difference in result set returned by MySQL?
- Order date records and fetch the 2nd ordered record in MySQL
- How to get MySQL query result in same order as given by IN clause?
- What is the use of ORDER BY clause in MySQL?
- MySQL order by 0 first and then display the record in descending order?
- Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?
- How to use union and order by clause in MySQL?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- How to order or choose rows in MySQL GROUP BY clause?
- Sort by order of values in a MySQL select statement IN clause?
- How to replace 'Empty set' in a MySQL query?

Advertisements