Order by last 3 chars in MySQL?

MySQLMySQLi Database

You can use ORDER BY RIGHT() function to order by last 3 chars in MySQL. The syntax is as follows −

SELECT *FROM yourTableName
ORDER BY RIGHT(yourColumnName,3) yourSortingOrder;

Just replace the ‘yourSortingOrder’ to ASC or DESC to set the ascending or descending order respectively.

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table OrderByLast3Chars
   -> (
   -> EmployeeId int NOT NULL AUTO_INCREMENT,
   -> EmployeeName varchar(20),
   -> EmployeeAge int,
   -> PRIMARY KEY(EmployeeId)
   -> );
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('Carol_901',24);
Query OK, 1 row affected (0.14 sec)

mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('Bob_101',21);
Query OK, 1 row affected (0.19 sec)

mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('Sam_107',22);
Query OK, 1 row affected (0.20 sec)

mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('Mile_677',26);
Query OK, 1 row affected (0.19 sec)

mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('John_978',27);
Query OK, 1 row affected (0.75 sec)

mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('David_876',29);
Query OK, 1 row affected (0.28 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from OrderByLast3Chars;

The following is the output −

+------------+--------------+-------------+
| EmployeeId | EmployeeName | EmployeeAge |
+------------+--------------+-------------+
| 1          | Carol_901    | 24          |
| 2          | Bob_101      | 21          |
| 3          | Sam_107      | 22          |
| 4          | Mile_677     | 26          |
| 5          | John_978     | 27          |
| 6          | David_876    | 29          |
+------------+--------------+-------------+
6 rows in set (0.00 sec)

Here is the query to order by last 3 chars.

Case 1 − Get the result in ascending order.

The query is as follows −

mysql> select *from OrderByLast3Chars
   -> order by RIGHT(EmployeeName,3) asc;

The following is the output −

+------------+--------------+-------------+
| EmployeeId | EmployeeName | EmployeeAge |
+------------+--------------+-------------+
|          1 | Carol_901    |          24 |
|          2 | Bob_101      |          21 |
|          3 | Sam_107      |          22 |
|          4 | Mile_677     |          26 |
|          5 | John_978     |          27 |
|          6 | David_876    |          29 |
+------------+--------------+-------------+
6 rows in set (0.00 sec)

Case 2 − Get the result in descending order. The query is as follows −

mysql> select *from OrderByLast3Chars
   -> order by RIGHT(EmployeeName,3) desc;

The following is the output −

+------------+--------------+-------------+
| EmployeeId | EmployeeName | EmployeeAge |
+------------+--------------+-------------+
|          5 | John_978     |          27 |
|          1 | Carol_901    |          24 |
|          6 | David_876    |          29 |
|          4 | Mile_677     |          26 |
|          3 | Sam_107      |          22 |
|          2 | Bob_101      |          21 |
+------------+--------------+-------------+
6 rows in set (0.00 sec)
raja
Published on 01-Feb-2019 06:10:04
Advertisements