
- 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 desc except a single value in MySQL
Use ORDER BY and set DESC to order by desc. However, to get all the values except a single value, use the not equal operator.
Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.89 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('David'); 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 −
+-------+ | Name | +-------+ | Sam | | Chris | | John | | Carol | | Adam | | David | +-------+ 6 rows in set (0.00 sec)
Following is the query to order by desc except a single value i.e. Name “Adam” here −
mysql> select *from DemoTable ORDER BY (Name <> 'Adam') DESC,Name;
Output
This will produce the following output −
+-------+ | Name | +-------+ | Carol | | Chris | | David | | John | | Sam | | Adam | +-------+ +6 rows in set (0.00 sec)
- Related Articles
- Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query
- Resolve Syntax error near “ORDER BY order DESC” in MySQL?
- Fix a row value and then ORDER BY DESC rest of the values in MySQL
- Implement MySQL ORDER BY without using ASC or DESC?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- Display NULL and NOT NULL records except a single specific value in MySQL
- How to update all the entries except a single value in a particular column using MySQL?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Order by a function of two columns in a single MySQL query
- Is there a default ORDER BY value in MySQL?
- How to remove Duplicate Records except a single record in MySQL?
- ORDER BY specific field value first in MySQL
- How to use ORDER BY field and sort by id in a single MySQL field?
- How to order DESC by a field, but list the NULL values first?
- Set a certain value first with MySQL ORDER BY?

Advertisements