
- 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
Set a certain value first with MySQL ORDER BY?
Let us first create a table −
mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(14); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(19); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(103); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(87); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------+ | Number | +--------+ | 10 | | 20 | | 12 | | 14 | | 19 | | 45 | | 56 | | 103 | | 87 | | 98 | +--------+ 10 rows in set (0.00 sec)
Following is the query to order a certain value first with ORDER BY −
mysql− select *from DemoTable order by (Number=45) DESC,Number;
Output
This will produce the following output −
+--------+ | Number | +--------+ | 45 | | 10 | | 12 | | 14 | | 19 | | 20 | | 56 | | 87 | | 98 | | 103 | +--------+ 10 rows in set (0.00 sec)
- Related Articles
- ORDER BY specific field value first in MySQL
- MySQL ORDER BY with custom field value
- MySQL query to first set negative value in descending order and then positive value in ascending order
- MySQL query to order by the first number in a set of numbers?
- How to sort by value with MySQL ORDER BY?
- Order by date set with varchar type in MySQL
- How to order by certain part of a string in MySQL?
- Select with set order in MySQL
- ORDER BY alphabet first then follow by number in MySQL?
- Get records in a certain order using MySQL?
- MySQL Order by with case?
- Is there a default ORDER BY value in MySQL?
- Order by desc except a single value in MySQL
- Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query
- MySQL order by 0 first and then display the record in descending order?

Advertisements