
- 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
Implement ORDER BY in MySQL to order records in human readable format?
For this, use INET_ATON() in MySQL. Let’s say our records are in the form of an IP Address. The INET_ATON() method would allow a user to convert IP Address records to the number and then we can use ORDER BY to order them.
Let us first create a table −
mysql> create table DemoTable -> ( -> IpAddress varchar(50) -> ); Query OK, 0 rows affected (1.36 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('192.168.110.78'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('192.168.110.87'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('192.168.110.75'); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------------+ | IpAdress | +----------------+ | 192.168.110.78 | | 192.168.110.87 | | 192.168.110.75 | +----------------+ 3 rows in set (0.00 sec)
Here is the query to order by IP Address records −
mysql> select *from DemoTable -> order by inet_aton(IpAddress);
This will produce the following output −
+----------------+ | IpAddress | +----------------+ | 192.168.110.75 | | 192.168.110.78 | | 192.168.110.87 | +----------------+ 3 rows in set (0.00 sec)
- Related Articles
- Convert UNIX timestamp into human readable format in MySQL?
- How to order last 5 records by ID in MySQL
- ORDER BY records in MySQL based on a condition
- MySQL ORDER BY Date field not in date format?
- Order MySQL records randomly and display name in Ascending order
- Implement Custom Sort Order in MySQL
- How to order records by a column in MySQL and place empty records at the end?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- Order by a single field and display rest of the records in the same order with MySQL
- Implement MySQL ORDER BY without using ASC or DESC?
- Get records in a certain order using MySQL?
- Order records and delete n rows in MySQL
- MySQL query to ORDER BY records on the basis of modulus result
- Order VARCHAR records with string and numbers in MySQL
- Order a column in MySQL with IP Address records?

Advertisements