

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make MySQL result set the same as specified?
For this, use MySQL FIND_IN_SET(). Let us first create a table −
mysql> create table DemoTable1563 -> ( -> StudentId int, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1563 values(1001,'Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1563 values(1010,'Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1563 values(1005,'Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1563 values(1015,'David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1563 values(1030,'Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1563 values(1020,'Sam'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1563;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1001 | Chris | | 1010 | Bob | | 1005 | Chris | | 1015 | David | | 1030 | Mike | | 1020 | Sam | +-----------+-------------+ 6 rows in set (0.00 sec)
Here is the query to form MySQL result set the same as specified −
mysql> select * from DemoTable1563 -> where StudentId IN(1001,1030,1010,1020) -> order by find_in_set(StudentId,'1001,1030,1010,1020');
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1001 | Chris | | 1030 | Mike | | 1010 | Bob | | 1020 | Sam | +-----------+-------------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- How to get MySQL query result in same order as given by IN clause?
- Set options while creating a MySQL table. Display the same options as well
- How to select an empty result set in MySQL?
- How Can MySQL operator precedence affect result set?
- How can MySQL FIND_IN_SET() function be used to get the particular record(s) from the table as a result set?
- Set the result of a query to a variable in MySQL?
- How to create a MySQL table based on JDBC Result Set?
- How can we divide the result set returned by MySQL into groups?
- How can we get all the unique rows in MySQL result set?
- Setting column values as column names in the MySQL query result?
- In MySQL, how can we randomize set of rows or values in the result set?
- How to move the result set pointer to required position?
- How to make 'from' as column name in MySQL?
- What is the meaning of ‘empty set’ in MySQL result set?
- How to get file extension of file as a result of MySQL query?
Advertisements