
- 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 in a string mixed with numbers in MySQL?
Use ORDER BY with some cases. Let us create a table −
mysql> create table demo18 −> ( −> value text −> ); Query OK, 0 rows affected (1.18 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo18 values('John Smith'); Query OK, 1 row affected (0.06 sec) mysql> insert into demo18 values('2J John has 58'); Query OK, 1 row affected (0.17 sec) mysql> insert into demo18 values('2J John has 9'); Query OK, 1 row affected (0.09 sec)
Display records from the table using select statement −
mysql> select *from demo18;
This will produce the following output −
+----------------+ | value | +----------------+ | John Smith | | 2J John has 58 | | 2J John has 9 | +----------------+ 3 rows in set (0.00 sec)
Following is the query to sort −
mysql> select *from demo18 −> order by regexp_replace(value, '[0&minus9]*$', ''), −> length(value), −> value;
This will produce the following output −
+----------------+ | value | +----------------+ | 2J John has 9 | | 2J John has 58 | | John Smith | +----------------+ 3 rows in set (0.14 sec)
- Related Articles
- Sort character values from a column mixed with character and numbers in MySQL?
- Sort only numbers from alphanumeric string in MySQL?
- Alphanumeric Order by in MySQL for strings mixed with numbers
- Concatenate string with numbers in MySQL?
- How to sum a comma separated string (string with numbers) in MySQL?
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- MySQL order by string with numbers?
- Order VARCHAR records with string and numbers in MySQL
- MySQL sort string number?
- How to set a string with hyphen and numbers in MySQL varchar?
- How to select max of mixed string/int column in MySQL?
- Check if a string contains numbers in MySQL?
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- Sort items in MySQL with dots?
- How to sort mixed numeric/alphanumeric array in JavaScript

Advertisements