- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Selecting a value in custom order from another column in a MySQL table with a single query
For this, you can use IN().
Let us first create a table:
mysql> create table DemoTable727 ( Name varchar(100), Score int ); Query OK, 0 rows affected (0.88 sec)
Insert some records in the table using insert command:
mysql> insert into DemoTable727 values('Chris',45); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable727 values('Robert',89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable727 values('Carol',94); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable727 values('David',93); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable727 values('Mike',78); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable727 values('Sam',67); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement:
mysql> select *from DemoTable727;
This will produce the following output -
+--------+-------+ | Name | Score | +--------+-------+ | Chris | 45 | | Robert | 89 | | Carol | 94 | | David | 93 | | Mike | 78 | | Sam | 67 | +--------+-------+ 6 rows in set (0.00 sec)
Following is the query to select a value in custom order from another column in a MySQL table with a single query:
mysql> select Score from DemoTable727 where Name IN('Robert','David','Sam');
This will produce the following output -
+-------+ | Score | +-------+ | 89 | | 93 | | 67 | +-------+ 3 rows in set (0.00 sec)
- Related Articles
- Delete records from a MySQL table with IN() in a single query
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- Selecting and displaying only some rows from a column in a MySQL table
- Can we add a column to a table from another table in MySQL?
- MySQL query error with a table named “order”?
- Selecting a single row in MySQL?
- Subtracting a number from a single MySQL column value?
- Add a new column and index to an existing table with ALTER in a single MySQL query?
- A single MySQL query to select value from first table and insert in the second?
- Insert multiple values in a temporary table with a single MySQL query?
- Order randomly in MySQL with a random value column?
- Retrieve MIN and MAX date in a single MySQL query from a column with date values
- Set all the columns of a MySQL table to a particular value with a single query
- How to delete a single value from a MySQL table with duplicate records?

Advertisements