
- 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
How to order by certain part of a string in MySQL?
You can use ORDER BY SUBSTRING() to order by certain part of a string in MySQL. Let us first create a table:
mysql> create table DemoTable (UserId varchar(200)); Query OK, 0 rows affected (0.68 sec)
Following is the query to insert records in the table using insert command:
mysql> insert into DemoTable values('USER_1234'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('USER_John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('USER_Sam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('USER_Carol'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('USER_Bob'); Query OK, 1 row affected (0.14 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+------------+ | UserId | +------------+ | USER_1234 | | USER_John | | USER_Sam | | USER_Carol | | USER_Bob | +------------+ 5 rows in set (0.00 sec)
Case 1: When you want to ORDER BY part of a string in ascending order.
Following is the query. Here, we will order certain part of the string after 4th character:
mysql> select *from DemoTable order by substring(UserId,4) asc;
This will produce the following output
+------------+ | UserId | +------------+ | USER_1234 | | USER_Bob | | USER_Carol | | USER_John | | USER_Sam | +------------+ 5 rows in set (0.00 sec)
Case 2: When you want to ORDER BY part of a string in descending order. Following is the query:
mysql> select *from DemoTable order by substring(UserId,4) desc;
This will produce the following output. Here, we will order certain part of the string after 4th character:
+------------+ | UserId | +------------+ | USER_Sam | | USER_John | | USER_Carol | | USER_Bob | | USER_1234 | +------------+ 5 rows in set (0.00 sec)
- Related Articles
- How to Order by a specific string in MySQL?
- Set a certain value first with MySQL ORDER BY?
- Split the left part of a string by a separator string in MySQL?
- How to ORDER BY last 2 character string in MySQL?
- Fetch middle part of a string surrounded by slash in MySQL
- MySQL query to sort by certain last string character?
- Get records in a certain order using MySQL?
- How to cut part of a string with a MySQL query?
- MySQL query to change a string by displaying only the part of string after underscore?
- MySQL order by string with numbers?
- Creating String Object from certain part of a character Array in Java
- How to ORDER BY RELEVANCE in MySQL?
- How to ORDER BY LIKE in MySQL?
- How to order by auto_increment in MySQL?
- How to order by timestamp in MySQL?

Advertisements