Home Database Mysql Tutorial IP处理函数inet_aton()和inet_ntoa()使用说明

IP处理函数inet_aton()和inet_ntoa()使用说明

Jun 07, 2016 pm 06:06 PM
inet_aton

IP处理函数inet_aton()和inet_ntoa()使用说明,需要的朋友可以参考下

MYSql的IP对int的转换函数
select inet_aton(ip) from table_name;

网络地址:
192.168.33.123
每一个值最大不会越过255,也就是十六进制的FF,两个Byte刚好表示的最大值是255,
这样子,就可以用一个32位的整形来保存这个地址
192 168 33 123
1100 0000 1010 1000 0010 0001 0111 1011
把这些二进制合在一起就是32位的数了
11000000101010000010000101111011
十进制为
3232244091
* INET_ATON(expr)
给出一个作为字符串的网络地址的"点地址"(如127.0.0.1)表示,返回一个代表该地址数值的整数。地址可以是4或8比特地址。
mysql> SELECT INET_ATON('209.207.224.40');
-> 3520061480
产生的数字总是按照网络字节顺序。如上面的例子,数字按照 209×2^24 + 207×2^16 + 224×2^8 + 40 进行计算。
INET_ATON() 也能理解短格式 IP 地址:

mysql> SELECT INET_ATON('127.0.0.1'), INET_ATON('127.1');
-> 2130706433, 2130706433
注释: 在存储由INET_ATON() 产生的值时,推荐你使用 INT UNSIGNED 列。假如你使用 (带符号) INT列, 则相应的第一个八位组大于127的IP 地址值会被截至 2147483647 (即, INET_ATON('127.255.255.255') 所返回的值)。请参见11.2节,“数值类型”。
* INET_NTOA(expr)
给定一个数字网络地址 (4 或 8 比特),返回作为字符串的该地址的电地址表示。
mysql> SELECT INET_NTOA(3520061480);

-> '209.207.224.40'


在做项目时,并不知道这两个函数,所以对IP的处理都是自己写的函数,但是使用时效率却是十分的差,下面函数是把IP转化成整数:
代码如下:
CREATE FUNCTION `transIPtoInt`(ip char(31)) RETURNS char(31)
begin
DECLARE value1 CHAR(10);
DECLARE value2 CHAR(10);
DECLARE value3 CHAR(10);
DECLARE value4 CHAR(10);
set value1=SUBSTRING_INDEX(ip, '.', 1);
set value2=SUBSTRING_INDEX(ip, '.', 2);
set value2=SUBSTRING_INDEX(value2, '.', -1);
set value3=SUBSTRING_INDEX(ip, '.', -2);
set value3=SUBSTRING_INDEX(value3, '.', 1);
set value4=SUBSTRING_INDEX(ip, '.', -1);
set value1=value1set value2=value2set value3=value3return value1+value2+value3+value4;
end;

为了IP地址比较的方便,我还写了一个把IP地址中每一段都补足三位的函数,如下:
代码如下:
CREATE FUNCTION `fillIp`(ip char(31)) RETURNS char(31)
begin
DECLARE value1 CHAR(31);
DECLARE value2 CHAR(10);
DECLARE value3 CHAR(10);
DECLARE value4 CHAR(10);
set value1=SUBSTRING_INDEX(ip, '.', 1);
set value2=SUBSTRING_INDEX(ip, '.', 2);
set value2=SUBSTRING_INDEX(value2, '.', -1);
set value3=SUBSTRING_INDEX(ip, '.', -2);
set value3=SUBSTRING_INDEX(value3, '.', 1);
set value4=SUBSTRING_INDEX(ip, '.', -1);
set value1=LPAD(value1,3,'0');
set value2=LPAD(value2,3,'0');
set value3=LPAD(value3,3,'0');
set value4=LPAD(value4,3,'0');
return CONCAT(value1,'.',value2,'.',value3,'.',value4);
end;

以下是一些补充
* INET_ATON(expr)
给出一个作为字符串的网络地址的"点地址"(如127.0.0.1)表示,返回一个代表该地址数值的整数。地址可以是4或8比特地址。
mysql> SELECT INET_ATON('209.207.224.40');
-> 3520061480
产生的数字总是按照网络字节顺序。如上面的例子,数字按照 209×2563 + 207×2562 + 224×256 + 40 进行计算。
INET_ATON() 也能理解短格式 IP 地址:
mysql> SELECT INET_ATON('127.0.0.1'), INET_ATON('127.1');
-> 2130706433, 2130706433
注释: 在存储由INET_ATON() 产生的值时,推荐你使用 INT UNSIGNED 列。假如你使用 (带符号) INT列, 则相应的第一个八位组大于127的IP 地址值会被截至 2147483647 (即, INET_ATON('127.255.255.255') 所返回的值)。请参见MySQL文档中的“数值类型”。
* INET_NTOA(expr)
给定一个数字网络地址 (4 或 8 比特),返回作为字符串的该地址的电地址表示。
*
mysql> SELECT INET_NTOA(3520061480);
-> '209.207.224.40'

--------------------------------------------------------------------------------------------------

整型字段的比较比字符串效率高很多,这也符合一项优化原则:字段类型定义使用最合适(最小),最简单的数据类型。
inet_aton()算法,其实借用了国际上对各国IP地址的区分中使用的ip number。
a.b.c.d 的ip number是:
a * 256的3次方 + b * 256的2次方 + c * 256的1次方 + d * 256的0次方。

--------------------------------------------------------------------------------------------------

利用mysql的内置函数处理时间戳问题
eg : select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x');
结果: 2004 3rd August 03:35:48 2004
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles