
- 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 by character length in MySQL
To sort by character length in MySQL use the ORDER BY LENGTH(). Let us first create a table:
mysql> create table orderingAADemo -> ( -> Value varchar(100) -> ); Query OK, 0 rows affected (1.30 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into orderingAADemo values('A'); Query OK, 1 row affected (0.12 sec) mysql> insert into orderingAADemo values('B'); Query OK, 1 row affected (0.13 sec) mysql> insert into orderingAADemo values('AA'); Query OK, 1 row affected (0.20 sec) mysql> insert into orderingAADemo values('C'); Query OK, 1 row affected (0.12 sec) mysql> insert into orderingAADemo values('CCC'); Query OK, 1 row affected (0.22 sec)
Following is the query to display records from the table using select command:
mysql> select *from orderingAADemo;
This will produce the following output
+-------+ | Value | +-------+ | A | | B | | AA | | C | | CCC | +-------+ 5 rows in set (0.00 sec)
Following is the query to sort by character length in descending order:
mysql> select *from orderingAADemo -> order by length(Value) DESC, Value;
This will produce the following output
+-------+ | Value | +-------+ | CCC | | AA | | A | | B | | C | +-------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL query to sort by certain last string character?
- Python – Sort by Rear Character in Strings List
- Sort character values from a column mixed with character and numbers in MySQL?
- Python – Sort String list by K character frequency
- Python – Sort Matrix by Maximum String Length
- Sort String Array alphabetically by the initial character only in Java
- How to sort by arbitrary keywords in MySQL?
- Sort by date & time in descending order in MySQL?
- Order By Length of Column in MySQL
- Filter column value by the first character in MySQL
- MongoDB aggregation framework to sort by length of array?
- How to sort by value with MySQL ORDER BY?
- MySQL show tables sort by table name?
- How to ORDER BY last 2 character string in MySQL?
- Order strings by length of characters IN mYsql?

Advertisements