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/