Store IP addresses of the int type in mysql

Source: Internet
Author: User
This article has no technical knowledge. It mainly introduces two mysql functions. INET_ATON () converts the IP address into a decimal number, and INET_NTOA () converts the number to an IP address. Let's take a look at the IP address structure. 127.0.0.1: The computer is sensitive to the Npower of 2. At first glance, it can be guessed that it is composed of four 8-bit binary numbers. That's it! That is to say, IP Forwarding

This article has no technical knowledge. It mainly introduces two mysql functions. INET_ATON () converts the IP address into a decimal number, and INET_NTOA () converts the number to an IP address. Let's take a look at the IP address structure. 127.0.0.1: The computer is sensitive to the Npower of 2. At first glance, it can be guessed that it is composed of four 8-bit binary numbers. That's it! That is to say, IP Forwarding

This article has no technical knowledge. It mainly introduces two mysql functions. INET_ATON () converts the IP address into a decimal number, and INET_NTOA () converts the number to an IP address.

Let's take a look at the IP address structure. 127.0.0.1: The computer is sensitive to the Npower of 2. At first glance, it can be guessed that it is composed of four 8-bit binary numbers. That's it! That is to say, you can convert an IP address to a binary value as follows:

select concat(bin(127),lpad(bin(0),8,0),lpad(bin(0),8,0),lpad(bin(1),8,0));

Add 0 to the front of each digit, and then splice it to obtain the following result: 1111111000000000000000000000001

The system's INET_ATON () function is also calculated in this way. If you do not believe it, you can compare the result:

select bin(inet_aton('127.0.0.1'));

The result is also as follows: 1111111000000000000000000000001

So we come to the conclusion:INET_ATON () is to convert each segment of the IP into a binary string and then convert the 32-bit binary number into a 10-digit binary string.

Example:

Select INET_ATON ('192. 0.0.1 '); -- convert to the number select INET_NTOA (127); -- convert to IP

Why is this storage?

Some people say that space saving is also true, but it is a little painful. Is the current hard disk so cheap to care about this space?

A more reasonable explanation is that it is convenient to query. For example, you can query all IP addresses in a CIDR Block:

select * from table where ip between inet_aton('192.168.0.1') and inet_aton('192.168.0.255');

Disclaimer: This article uses the CC BY-NC-SA 3.0 protocol for authorization

Reprinted, please indicate the source: Xiaojing's blog

Link: http://www.phpv5.com/blog/archives/313/

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.