
- 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 specific field value first in MySQL
To order by specific field value first in MySQL, use ORDER BY FIELD(). Let us first create a table −
mysql> create table DemoTable849(Color varchar(100)); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable849 values('RED'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable849 values('ORANGE'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable849 values('BLUE'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable849 values('GREEN'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable849;
This will produce the following output −
+--------+ | Color | +--------+ | RED | | ORANGE | | BLUE | | GREEN | +--------+ 4 rows in set (0.00 sec)
Following is the query to order by specific value first −
mysql> select *from DemoTable849 order by field(Color,'RED','GREEN','BLUE','ORANGE');
This will produce the following output −
+--------+ | Color | +--------+ | RED | | GREEN | | BLUE | | ORANGE | +--------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL ORDER BY with custom field value
- Set a certain value first with MySQL ORDER BY?
- MySQL Order By specific strings?
- ORDER BY a specific word in MySQL
- How to swap a specific field value in MySQL?
- Order by on a specific number in MySQL?
- MySQL order by field using CASE Statement
- MySQL ORDER BY Date field not in date format?
- Simulating MySQL's ORDER BY FIELD() in PostgreSQL?
- How to Order by a specific string in MySQL?
- ORDER BY alphabet first then follow by number in MySQL?
- How to use ORDER BY field and sort by id in a single MySQL field?
- MySQL query to first set negative value in descending order and then positive value in ascending order
- MySQL IF/WHEN/ELSE/OR with ORDER BY FIELD
- MySQL Order by a specific column x and display remaining values in ascending order

Advertisements