
- 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
Order by number of chars in MySQL?
To order by number of chars, use ORDER BY and LENGTH() method. Following is the syntax −
select *from yourTableName order by LENGTH(yourColumnName) DESC;
Let us first create a table −
mysql− create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------+ | Name | +--------+ | John | | Robert | | Bob | | David | +--------+ 4 rows in set (0.00 sec)
Following is the query to order by number of chars −
mysql> select *from DemoTable -> order by LENGTH(Name) DESC;
Output
This will produce the following output −
+--------+ | Name | +--------+ | Robert | | David | | John | | Bob | +--------+ 4 rows in set (0.00 sec)
- Related Articles
- Order by last 3 chars in MySQL?
- Order by on a specific number in MySQL?
- ORDER BY alphabet first then follow by number in MySQL?
- MySQL query to order by the first number in a set of numbers?
- Order By Length of Column in MySQL
- Order strings by length of characters IN mYsql?
- Order By date ASC in MySQL?
- Order by selected record in MySQL?
- MySQL select order by acts like a string (not a number)?
- Resolve Syntax error near “ORDER BY order DESC” in MySQL?
- MySQL Order By specific strings?
- MySQL Order by with case?
- MySQL Order by beginning letter?
- How to ORDER BY RELEVANCE in MySQL?
- How to ORDER BY LIKE in MySQL?

Advertisements