MySQL sorts IP addresses

Source: Internet
Author: User

Use the built-in functions of mysql:

INET_ATON (expr)
Given the dotted-quad representation of a network address as a string, returns an integer that represents the numeric value of the address. Addresses may be 4-or 8-byte addresses.
Mysql> SELECT INET_ATON ('2017. 207.224.40 ');
-> 3520061480
The generated number is always in network byte order. For the example just shown, the number is calculated as 209 × 2563 + 207 × 2562 + 224 × 256 + 40.
INET_ATON () also understands short-formIPaddresses:
Mysql> SELECT INET_ATON ('2014. 0.0.1 '), INET_ATON ('2014. 1 ');
-> 2130706433,213 0706433
Note: When storing values generated by INET_ATON (), it is recommended that you use an int unsigned column. if you use a (signed) INT column, values corresponding toIPaddresses for which the first octet is greater than 127 cannot be stored correctly. see Section 11.2, "Numeric Types ".

INET_NTOA (expr)
Given a numeric network address (4 or 8 byte), returns the dotted-quad representation of the address as a string.
Mysql> SELECT INET_NTOA (3520061480 );
-> '2014. 207.224.40'

The above is the description in the Mysql Manual. The actual usage is as follows:

My ip address is stored as a string in the ip_info field of table table_ip. You can use the following query statement to return the results sorted by ip address.
Select ip_info from table_ip order by inet_aton (ip_info );
Result:
192.168.6.10
192.168.6.60
92.166.120
92.166.240
...

Recommended reading:

MySQL execution plan changes as the data volume increases

Login in MySQL database security mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.