
- 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
Maintaining order in MySQL âINâ query?
You can maintain the order in MySQL IN query with the help of field command. The syntax is as follows −
select *from yourTableName anyVariableName where anyVariableName.yourColumnName in(value1,value2,......N) order by field(anyVariableName.yourColumnName,value1,value2,......N);
To implement the above syntax let us create a table −
mysql> create table OrderInDemo −> ( −> Id int, −> Name varchar(100), −> Age int −> ); Query OK, 0 rows affected (1.24 sec)
Now let us insert some records in the table. The query to insert records are as follows −
mysql> insert into OrderInDemo values(90,'David',23); Query OK, 1 row affected (0.10 sec) mysql> insert into OrderInDemo values(9,'Sam',24); Query OK, 1 row affected (0.59 sec) mysql> insert into OrderInDemo values(10,'Carol',19); Query OK, 1 row affected (0.25 sec) mysql> insert into OrderInDemo values(1,'John',26); Query OK, 1 row affected (0.42 sec) mysql> insert into OrderInDemo values(3,'Johnson',25); Query OK, 1 row affected (0.18 sec) mysql> insert into OrderInDemo values(2,'Ramit',20); Query OK, 1 row affected (0.18 sec)
Display all records with the help of select statement. The query is as follows −
mysql> select *from OrderInDemo;
The following is the output −
+------+---------+------+ | Id | Name | Age | +------+---------+------+ | 90 | David | 23 | | 9 | Sam | 24 | | 10 | Carol | 19 | | 1 | John | 26 | | 3 | Johnson | 25 | | 2 | Ramit | 20 | +------+---------+------+ 6 rows in set (0.00 sec)
Implement the syntax we discussed, in the beginning, to maintain order in MySQL IN query. The query is as follows −
mysql> select *from OrderInDemo OD where OD.Id in(10,1,3) −> order by field(OD.Id,10,1,3);
The following is the output that displays the results ordered in the sequence provided in the query −
+------+---------+------+ | Id | Name | Age | +------+---------+------+ | 10 | Carol | 19 | | 1 | John | 26 | | 3 | Johnson | 25 | +------+---------+------+ 3 rows in set (0.00 sec)
- Related Articles
- Python - Inserting item in sorted list maintaining order
- Order MySQL query by multiple ids?
- MySQL query to display ASC order in number column?
- MySQL query to order by two fields and NULL values in chronological order?
- How to order and select query with conditions in MySQL?
- MySQL query to order by NULL values
- How to place number 0 from a column at the end maintaining the ascending search order in MySQL?
- Write a MySQL query equivalent to âSHOW TABLESâ in sorted order?
- MySQL query to select rows except first row in descending order?
- MySQL query to select distinct order by id
- MySQL query error with a table named âorderâ?
- MySQL query to first set negative value in descending order and then positive value in ascending order
- Order by a function of two columns in a single MySQL query
- How can you order the result obtained by select query in MySQL?
- MySQL query to order by current day and month?

Advertisements