
- 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
How to get MySQL query result in same order as given by IN clause?
For this, you can use IN() along with ORDER BY FIELD(). Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100) ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName) values('Sam'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Carol'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.42 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Robert | | 3 | Mike | | 4 | Sam | | 5 | Carol | | 6 | David | +----+-----------+ 6 rows in set (0.00 sec)
The following is how to get query results in the same order as given by the IN clause.
mysql> select FirstName from DemoTable where Id IN(4,5,6) order by field(Id,4,5,6);
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Sam | | Carol | | David | +-----------+ 3 rows in set (0.00 sec)
- Related Articles
- How can you order the result obtained by select query in MySQL?
- How to use union and order by clause in MySQL?
- How to get file extension of file as a result of MySQL query?
- How to order or choose rows in MySQL GROUP BY clause?
- MySQL query to ORDER BY records on the basis of modulus result
- How to use actual row count (COUNT(*)) in WHERE clause without writing the same query as subquery in MySql?
- Get the returned record set order in MySQL IN clause?
- Get the returned record set ordered by (ORDER BY) position in MySQL 'IN' clause
- MySQL query to get result by month and year based on condition?
- Can we return query results in same order as the values in MySQL `IN(…)` statement?
- How to make MySQL result set the same as specified?
- MySQL query to get result from multiple select statements?
- What is the use of ORDER BY clause in MySQL?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- While using the ROLLUP modifier, is it possible to use a MySQL ORDER BY clause to sort the result?

Advertisements