
- 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
MySQL query to sort by certain last string character?
For this, you can use the CASE statement. To sort, use the ORDER BY clause. Let us first create a table −
mysql> create table DemoTable -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(20) -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command. Some records have certain last string like -D, etc −
mysql> insert into DemoTable(ClientName) values('Mike'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(ClientName) values('John'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(ClientName) values('John-D'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ClientName) values('John-Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(ClientName) values('Mike-Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ClientName) values('Mike-D'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 1 | Mike | | 2 | John | | 3 | John-D | | 4 | John-Smith | | 5 | Mike-Smith | | 6 | Mike-D | +----------+------------+ 6 rows in set (0.00 sec)
Here is the query to sort by certain last string character −
mysql> select -> case -> when right(ClientName,length(ClientName)-instr(ClientName,'-')) = `ClientName` THEN '' -> else right(`ClientName`,length(`ClientName`)-INSTR(`ClientName`,'-')) -> end as `last`, -> `ClientName` -> from -> DemoTable -> ORDER BY -> `last`,`ClientName`;
This will produce the following output −
+-------+------------+ | last | ClientName | +-------+------------+ | | John | | | Mike | | D | John-D | | D | Mike-D | | Smith | John-Smith | | Smith | Mike-Smith | +-------+------------+ 6 rows in set (0.00 sec)
- Related Articles
- Getting last 5 character of a string with MySQL query?
- How to ORDER BY last 2 character string in MySQL?
- MySQL query to select column values ending with certain character/number?
- Sort by character length in MySQL
- Python – Sort String list by K character frequency
- MySQL query to sort by both timestamp and enum?
- MySQL Query to remove all characters after last comma in string?
- Sort certain values to the top in MySQL?
- How to order by certain part of a string in MySQL?
- Sort String Array alphabetically by the initial character only in Java
- MongoDB query to sort by words
- MySQL query to display a substring before a special character in a string
- MySQL query to update string field by concatenating to it?
- MySQL query to get a substring from a string except the last three characters?
- MySQL query to split the string “Learn With Ease” and return the last word?

Advertisements