
- 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
Can we return query results in same order as the values in MySQL `IN(…)` statement?
Yes, you can achieve this with ORDER BY FIELD() from MySQL. Let us first create a table −
mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(19); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(34); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(28); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(25); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(29); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(24); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 19 | | 30 | | 34 | | 28 | | 25 | | 29 | | 24 | +--------+ 7 rows in set (0.00 sec)
Following is the query to return result in the same order as the values set −
mysql> select *from DemoTable -> ORDER BY FIELD(Number, 30,19,34,25,28,29,24) ;
This will produce the following output displaying the results in the same order −
+--------+ | Number | +--------+ | 30 | | 19 | | 34 | | 25 | | 28 | | 29 | | 24 | +--------+ 7 rows in set (0.00 sec)
- Related Articles
- How can we enter numeric values as Hexadecimal (HEX) number in MySQL statement?
- How can we enter numerical values as a BINARY number in MySQL statement?
- How can we enter BOOLEAN values in MySQL statement?
- Can we use SELECT NULL statement in a MySQL query?
- How can we specify default values in MySQL INSERT statement?
- How to get MySQL query result in same order as given by IN clause?
- How can we update any value in MySQL view as we can update the values in MySQL table?
- Can we have a return statement in a JavaScript switch statement?
- MySQL query to return a string as a result of IF statement?
- How can we enter characters as Hexadecimal (HEX) number in MySQL statement?
- How can we enter characters as a BINARY number in MySQL statement?
- Get a fixed number of results in descending order using a MySQL query
- How to order results of a query randomly & select random rows in MySQL?
- Sort by order of values in a MySQL select statement IN clause?
- MySQL query to order by two fields and NULL values in chronological order?

Advertisements