
- 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
MySQL Order By specific strings?
Order by the choice of strings you want, using the FIELD() function. The syntax is as follows −
SELECT *FROM yourTableName ORDER BY FIELD(yourColumnName,’yourValue1’, ’yourValue2’,’yourValue3’,....N);
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table OrderByListOfStrings -> ( -> Id int not null auto_increment, -> CarName varchar(20), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into OrderByListOfStrings(CarName) values('Ford'); Query OK, 1 row affected (0.18 sec) mysql> insert into OrderByListOfStrings(CarName) values('Audi'); Query OK, 1 row affected (0.17 sec) mysql> insert into OrderByListOfStrings(CarName) values('Honda'); Query OK, 1 row affected (0.13 sec) mysql> insert into OrderByListOfStrings(CarName) values('Aston Martin'); Query OK, 1 row affected (0.53 sec) mysql> insert into OrderByListOfStrings(CarName) values('Bugatti'); Query OK, 1 row affected (0.20 sec) mysql> insert into OrderByListOfStrings(CarName) values('BMW'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from OrderByListOfStrings;
The following is the output −
+----+--------------+ | Id | CarName | +----+--------------+ | 1 | Ford | | 2 | Audi | | 3 | Honda | | 4 | Aston Martin | | 5 | Bugatti | | 6 | BMW | +----+--------------+ 6 rows in set (0.00 sec)
Here is the query to get the order by the choice of strings. Set in whatever order you want them −
mysql> select *from OrderByListOfStrings -> order by field(CarName,'Bugatti','BMW','Audi','Aston Martin','Ford','Honda');
The following is the output that rearranges string set with field() method −
+----+--------------+ | Id | CarName | +----+--------------+ | 5 | Bugatti | | 6 | BMW | | 2 | Audi | | 4 | Aston Martin | | 1 | Ford | | 3 | Honda | +----+--------------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL ORDER BY strings with underscore?
- ORDER BY a specific word in MySQL
- Order strings by length of characters IN mYsql?
- ORDER BY specific field value first in MySQL
- Order by on a specific number in MySQL?
- How to Order by a specific string in MySQL?
- Alphanumeric Order by in MySQL for strings mixed with numbers
- MySQL Order by a specific column x and display remaining values in ascending order
- MySQL Order by with case?
- MySQL Order by beginning letter?
- How to arrange data in s specific order in MySQL?
- Advanced sorting in MySQL to display strings beginning with J at the end even after ORDER BY
- Order By date ASC in MySQL?
- Order by selected record in MySQL?
- Order MySQL query by multiple ids?

Advertisements