MySQL IP转数字函数 INET_ATON() INET_NTOA()

934 total views, 1 views today

在MySQL中IP地址可以使用varchar字段类型存储,但是若查找某个IP区间的IP地址就很麻烦。

但是通过 INET_ATON() 函数将IP地址转换为数字(二进制数字格式)就能实现IP地址比较的功能。

inet_aton() converts the Internet host address cp from the IPv4 numbers-and-dots notation into binary form
INET_ATON() 将IP地址转换为二进制。 ATON= address to number (个人猜测,为了好记)。

INET_NTOA() 将二进制转为IP地址。NTOA = number to address。

 

例子:使用 inet_aton()函数将IP地址转换成数字存入表中,然后查找某个范围内的IP地址

创建表并存入转换后的IP地址

查找在 10.0.2.99 与 10.0.2.103之间的IP地址

发表评论

必填项已用*标注