MySQL inet aton函数,MySQL IP转数字函数 INET_ATON() INET_NTOA()

223 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 (个人猜测,为了好记)。

mysql> SELECT inet_aton('10.0.2.103');

+-------------------------+

| inet_aton('10.0.2.103') |

+-------------------------+

| 167772775 |

+-------------------------+

1 row in set (0.00 sec)

1

2

3

4

5

6

7

mysql>SELECTinet_aton('10.0.2.103');

+-------------------------+

|inet_aton('10.0.2.103')|

+-------------------------+

|167772775|

+-------------------------+

1rowinset(0.00sec)

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

mysql> SELECT INET_NTOA(167772775);

+----------------------+

| INET_NTOA(167772775) |

+----------------------+

| 10.0.2.103 |

+----------------------+

1 row in set (0.00 sec)

1

2

3

4

5

6

7

mysql>SELECTINET_NTOA(167772775);

+----------------------+

|INET_NTOA(167772775)|

+----------------------+

|10.0.2.103|

+----------------------+

1rowinset(0.00sec)

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

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

create table tab_ips(ip_address int);

insert into tab_ips values(inet_aton('10.0.2.99'));

insert into tab_ips values(inet_aton('10.0.2.100'));

insert into tab_ips values(inet_aton('10.0.2.101'));

insert into tab_ips values(inet_aton('10.0.2.103'));

insert into tab_ips values(inet_aton('10.0.2.104'));

select * from tab_ips;

+------------+

| ip_address |

+------------+

| 167772771 |

| 167772772 |

| 167772773 |

| 167772775 |

| 167772776 |

+------------+

5 rows in set (0.00 sec)

转成IP地址

select inet_ntoa(ip_address) from tab_ips;

+-----------------------+

| inet_ntoa(ip_address) |

+-----------------------+

| 10.0.2.99 |

| 10.0.2.100 |

| 10.0.2.101 |

| 10.0.2.103 |

| 10.0.2.104 |

+-----------------------+

5 rows in set (0.00 sec)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

createtabletab_ips(ip_addressint);

insertintotab_ipsvalues(inet_aton('10.0.2.99'));

insertintotab_ipsvalues(inet_aton('10.0.2.100'));

insertintotab_ipsvalues(inet_aton('10.0.2.101'));

insertintotab_ipsvalues(inet_aton('10.0.2.103'));

insertintotab_ipsvalues(inet_aton('10.0.2.104'));

select *fromtab_ips;

+------------+

|ip_address|

+------------+

|167772771|

|167772772|

|167772773|

|167772775|

|167772776|

+------------+

5rowsinset(0.00sec)

转成IP地址

selectinet_ntoa(ip_address)fromtab_ips;

+-----------------------+

|inet_ntoa(ip_address)|

+-----------------------+

|10.0.2.99|

|10.0.2.100|

|10.0.2.101|

|10.0.2.103|

|10.0.2.104|

+-----------------------+

5rowsinset(0.00sec)

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

select inet_ntoa(ip_address) from tab_ips where ip_address>inet_aton('10.0.2.99') and ip_address

+-----------------------+

| inet_ntoa(ip_address) |

+-----------------------+

| 10.0.2.100 |

| 10.0.2.101 |

+-----------------------+

2 rows in set (0.02 sec)

1

2

3

4

5

6

7

8

selectinet_ntoa(ip_address)fromtab_ipswhereip_address>inet_aton('10.0.2.99')andip_address

+-----------------------+

|inet_ntoa(ip_address)|

+-----------------------+

|10.0.2.100|

|10.0.2.101|

+-----------------------+

2rowsinset(0.02sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值