
- 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
Display IDs in a particular order with MySQL IN()?
To display IDs in a particular order i.e. the order of your choice use FIELD() method.
Let us first create a table −
mysql> create table DemoTable ( UserId int ); Query OK, 0 rows affected (0.64 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.13 sec)
Following is the query to display records from the table using select command −
mysql> select *from DemoTable;
This will produce the following output
+--------+ | UserId | +--------+ | 100 | | 10 | | 40 | | 80 | | 70 | | 60 | +--------+ 6 rows in set (0.00 sec)
Following is the query to use IN() and FIELD() to display records in the order of your choice −
mysql> select UserId from DemoTable where UserId IN(60,100,10,80,40,70) order by field(UserId,60,100,10,80,40,70);
This will produce the following output −
+--------+ | UserId | +--------+ | 60 | | 100 | | 10 | | 80 | | 40 | | 70 | +--------+ 6 rows in set (0.03 sec)
- Related Articles
- Count duplicate ids and display the result in a separate column with MySQL
- Order MySQL query by multiple ids?
- MySQL - SELECT … WHERE id IN (..) order with particular column?
- Display highest amount from corresponding duplicate ids in MySQL
- Display records after a particular date in MySQL
- MySQL CASE WHEN with SELECT to display odd and even ids?
- Maintain the custom order of the IDs passed in MySQL
- Update table with duplicate ids in MySQL
- Order by a single field and display rest of the records in the same order with MySQL
- Order MySQL records randomly and display name in Ascending order
- MySQL Order by a specific column x and display remaining values in ascending order
- Display collections in a particular MongoDB database?
- MySQL query to display ASC order in number column?
- MySQL order by 0 first and then display the record in descending order?
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL

Advertisements