
- 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
ORDERBY word in MySQL?
To order by word in MySQL, you need to use ORDER BY FIELD(). Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(20), StudentFavouriteSubject varchar(100) ); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentFirstName,StudentFavouriteSubject) values('Larry','Java'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentFirstName,StudentFavouriteSubject) values('Sam','C'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(StudentFirstName,StudentFavouriteSubject) values('Bob','MongoDB'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentFirstName,StudentFavouriteSubject) values('David','MySQL'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement;
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+-------------------------+ | StudentId | StudentFirstName | StudentFavouriteSubject | +-----------+------------------+-------------------------+ | 1 | Larry | Java | | 2 | Sam | C | | 3 | Bob | MongoDB | | 4 | David | MySQL | +-----------+------------------+-------------------------+ 4 rows in set (0.00 sec)
Following is the query to ORDER BY word in MySQL −
mysql> select *from DemoTable ORDER BY FIELD(`StudentFavouriteSubject`, 'MongoDB','MySQL','Java','C');
This will produce the following output −
+-----------+------------------+-------------------------+ | StudentId | StudentFirstName | StudentFavouriteSubject | +-----------+------------------+-------------------------+ | 3 | Bob | MongoDB | | 4 | David | MySQL | | 1 | Larry | Java | | 2 | Sam | C | +-----------+------------------+-------------------------+ 4 rows in set (0.03 sec)
- Related Articles
- Orderby clause in C#
- C# orderby Keyword
- C# Orderby Descending
- Select first word in a MySQL query?
- ORDER BY a specific word in MySQL
- Is name a reserved word in MySQL?
- How to Use OrderBy for Multiple Columns in Laravel?
- Sort a column ignoring a specific word in MySQL
- How to search specific word in MySQL with RegExp?
- Remove specific word in a comma separated string with MySQL
- MySQL query to search exact word from string?
- How to use ORDERBY in MongoDB if there are possible null values?
- MySQL query to extract last word from a field?
- MySQL query to extract first word from a field?
- A single MongoDB query to orderby date and group by user

Advertisements