
- 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
Implement MySQL ORDER BY without using ASC or DESC?
For this, you can use FIND_IN_SET(). Let us first create a table −
mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (2.25 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.91 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.69 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.72 sec) mysql> insert into DemoTable values(800); Query OK, 1 row affected (0.75 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 20 | | 60 | | 40 | | 800 | +--------+ 4 rows in set (0.00 sec)
Here is the query to implement MySQL ORDER BY without using ASC or DESC.
mysql> select *from DemoTable -> order by FIND_IN_SET(Number,'40,20,800,60');
This will produce the following output −
+--------+ | Number | +--------+ | 40 | | 20 | | 800 | | 60 | +--------+ 4 rows in set (0.14 sec)
- Related Articles
- Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query
- How to sort varchar numeric columns by DESC or ASC in MySQL?
- Order By date ASC in MySQL?
- Resolve Syntax error near “ORDER BY order DESC” in MySQL?
- Order by desc except a single value in MySQL
- MySQL ORDER BY ASC and display NULLs at the bottom?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- MySQL query to display ASC order in number column?
- What would be the effect on summary output when I use explicit sort order (ASC or DESC) with column names in the GROUP BY list along with “WITH ROLLUP” modifier?
- Fix a row value and then ORDER BY DESC rest of the values in MySQL
- Implement ORDER BY in MySQL to order records in human readable format?
- Select last 3 rows from database order by id ASC?
- Displaying only a list of records in ASC order with MySQL
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- Order MySQL results without identifier?

Advertisements