Sort the IP values using the INET_ATON( ) function, which converts a network address directly to its underlyin : INET_ATON « Function « SQL / MySQL






Sort the IP values using the INET_ATON( ) function, which converts a network address directly to its underlyin

      
g numeric form
mysql>
mysql> CREATE TABLE hostip
    -> (
    ->  ip      VARCHAR(64)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO hostip (ip)
    ->  VALUES
    ->          ('127.0.0.1'),
    ->          ('192.168.0.2'),
    ->          ('192.168.0.10'),
    ->          ('192.168.1.2'),
    ->          ('192.168.1.10'),
    ->          ('255.255.255.255'),
    ->          ('21.0.0.1')
    -> ;
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT ip FROM hostip ORDER BY INET_ATON(ip);
+-----------------+
| ip              |
+-----------------+
| 21.0.0.1        |
| 127.0.0.1       |
| 192.168.0.2     |
| 192.168.0.10    |
| 192.168.1.2     |
| 192.168.1.10    |
| 255.255.255.255 |
+-----------------+
7 rows in set (0.00 sec)

mysql>
mysql> drop table hostip;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
  








Related examples in the same category

1.To convert a network address to a numeric value, use INET_ATON() function: INET_ATON()
2.Convert network integer to an IP address by using the following function: INET_NTOA()