
- 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 alphabet first then follow by number in MySQL?
You need to use regular expression with ORDER BY clause. The syntax is as follows:
SELECT *FROM yourTableName ORDER BY IF(yourColumnName RLIKE '^[a-z]', 1, 2),yourColumnName;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table AlphabetFirstThenNumberDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(20), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.95 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into AlphabetFirstThenNumberDemo(Name) values('John'); Query OK, 1 row affected (0.37 sec) mysql> insert into AlphabetFirstThenNumberDemo(Name) values('Bob'); Query OK, 1 row affected (0.21 sec) mysql> insert into AlphabetFirstThenNumberDemo(Name) values('1Sam'); Query OK, 1 row affected (0.17 sec) mysql> insert into AlphabetFirstThenNumberDemo(Name) values('Carol'); Query OK, 1 row affected (0.22 sec) mysql> insert into AlphabetFirstThenNumberDemo(Name) values('2Larry'); Query OK, 1 row affected (0.31 sec) mysql> insert into AlphabetFirstThenNumberDemo(Name) values('David'); Query OK, 1 row affected (0.27 sec) mysql> insert into AlphabetFirstThenNumberDemo(Name) values('3Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into AlphabetFirstThenNumberDemo(Name) values('Robert'); Query OK, 1 row affected (0.10 sec)
Now you can display all records from the table using select statement. The query is as follows:
mysql> select *from AlphabetFirstThenNumberDemo;
The following is the output:
+----+--------+ | Id | Name | +----+--------+ | 1 | John | | 2 | Bob | | 3 | 1Sam | | 4 | Carol | | 5 | 2Larry | | 6 | David | | 7 | 3Mike | | 8 | Robert | +----+--------+ 8 rows in set (0.00 sec)
Here is the query order by alphabet first then followed by a number. The query is as follows:
mysql> select *from AlphabetFirstThenNumberDemo -> ORDER BY IF(Name RLIKE '^[a-z]', 1, 2),Name;
The following is the output:
+----+--------+ | Id | Name | +----+--------+ | 2 | Bob | | 4 | Carol | | 6 | David | | 1 | John | | 8 | Robert | | 3 | 1Sam | | 5 | 2Larry | | 7 | 3Mike | +----+--------+ 8 rows in set (0.00 sec)
- Related Articles
- Order by last 3 months first, then alphabetically in MySQL?
- MySQL order by 0 first and then display the record in descending order?
- ORDER BY specific field value first in MySQL
- MySQL query to order by the first number in a set of numbers?
- Order by number of chars in MySQL?
- Order by on a specific number in MySQL?
- Set a certain value first with MySQL ORDER BY?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- Order By date ASC in MySQL?
- Order by selected record in MySQL?
- MySQL Order By specific strings?
- MySQL Order by with case?
- MySQL Order by beginning letter?
- How to ORDER BY RELEVANCE in MySQL?
- How to ORDER BY LIKE in MySQL?

Advertisements