
- 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
Sort a MySQL table column value by part of its value?
You can use ORDER BY RIGHT() for this. Let us first create a table −
mysql> create table DemoTable ( UserId varchar(100) ); Query OK, 0 rows affected (0.33 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('User1234'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('User9874'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values('User9994'); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable values('User1211'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('User1012'); Query OK, 1 row affected (0.79 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output -
+----------+ | UserId | +----------+ | User1234 | | User9874 | | User9994 | | User1211 | | User1012 | +----------+ 5 rows in set (0.00 sec)
Case 1 − If you want the result in ascending order.
Following is the query to sort a MySQL table column value by part of its value.
mysql> select *from DemoTable ORDER BY RIGHT(UserId, 4);
This will produce the following output −
+----------+ | UserId | +----------+ | User1012 | | User1211 | | User1234 | | User9874 | | User9994 | +----------+ 5 rows in set (0.00 sec)
Case 2 − If you want the result in descending order.
Following is the query to sort a MySQL table column value by part of its value -
mysql> select *from DemoTable ORDER BY RIGHT(UserId, 4) DESC;
This will produce the following output −
+----------+ | UserId | +----------+ | User9994 | | User9874 | | User1234 | | User1211 | | User1012 | +----------+ 5 rows in set (0.00 sec)
- Related Articles
- Update a column value, replacing part of a string in MySQL?
- How to substring value in a MySQL table column?
- Updating a MySQL table row column by appending a value from user defined variable?
- Setting similar value for a column in a MySQL table?
- How to sort by value with MySQL ORDER BY?
- Replace part of string in MySQL table column?
- Sort data column to retrieve max textual value in MySQL
- Check if a value exists in a column in a MySQL table?
- Add some value to an int type column value in a table without knowing its current value in SQL?
- How to Sort object of objects by its key value JavaScript
- How to SELECT min and max value from the part of a table in MySQL?
- How to perform custom sort by field value in MySQL?
- How can we extract a substring from the value of a column in MySQL table?
- Group MySQL rows in an array by column value?
- Filter column value by the first character in MySQL

Advertisements