
- 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
Order by on a specific number in MySQL?
Let us first create a table −
mysql> create table DemoTable -> ( -> status int -> ); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------+ | status | +--------+ | 30 | | 20 | | 20 | | 30 | | 10 | | 40 | +--------+ 6 rows in set (0.00 sec)
Now, let’s say you want to use ORDER BY in a way to get the following result. Here, the number 10 is set in the end and rest of the list is ordered in ascending order.
20 20 30 30 40 10
Here is the query to implement order by clause on a specific number in MySQL.
mysql> select *from DemoTable order by status=10,status;
Output
+--------+ | status | +--------+ | 20 | | 20 | | 30 | | 30 | | 40 | | 10 | +--------+ 6 rows in set (0.00 sec)
- Related Articles
- ORDER BY a specific word in MySQL
- MySQL Order By specific strings?
- How to Order by a specific string in MySQL?
- ORDER BY specific field value first in MySQL
- MySQL Order by a specific column x and display remaining values in ascending order
- Order by number of chars in MySQL?
- ORDER BY records in MySQL based on a condition
- ORDER BY alphabet first then follow by number in MySQL?
- MySQL select order by acts like a string (not a number)?
- MySQL query to order by the first number in a set of numbers?
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- How to arrange data in s specific order in MySQL?
- Order By date ASC in MySQL?
- Order by selected record in MySQL?
- Select random number from a specific list in MySQL?

Advertisements