利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方法分享

时间:2024-01-16 17:28:39 

mysql> create table jackbillow (ip int unsigned, name char(1));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into jackbillow values(inet_aton('192.168.1.200'), 'A'), (inet_aton('200.100.30.241'), 'B');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> insert into jackbillow values(inet_aton('24.89.35.27'), 'C'), (inet_aton('100.200.30.22'), 'D');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from jackbillow;
+------------+------+
| ip | name |
+------------+------+
| 3232235976 | A |
| 3362004721 | B |
| 408494875 | C |
| 1690836502 | D |
+------------+------+
4 rows in set (0.00 sec)

mysql> select * from jackbillow where ip = inet_aton('192.168.1.200');
+------------+------+
| ip | name |
+------------+------+
| 3232235976 | A |
+------------+------+
1 row in set (0.00 sec)

mysql> select inet_ntoa(ip) from jackbillow;
+----------------+
| inet_ntoa(ip) |
+----------------+
| 192.168.1.200 |
| 200.100.30.241 |
| 24.89.35.27 |
| 100.200.30.22 |
+----------------+
4 rows in set (0.00 sec)

当前很多应用都适用字符串char(15)来存储IP地址(占用16个字节),利用inet_aton()和inet_ntoa()函数,来存储IP地址效率很高,适用unsigned int 就可以满足需求,不需要使用bigint,只需要4个字节,节省存储空间,同时效率也高很多。

如果IP列有索引,可以使用下面方式查询:

mysql> select inet_aton('100.200.30.22');
+----------------------------+
| inet_aton('100.200.30.22') |
+----------------------------+
| 1690836502 |
+----------------------------+
1 row in set (0.00 sec)

mysql> select * from jackbillow where ip=1690836502;
+------------+------+
| ip | name |
+------------+------+
| 1690836502 | D |
+------------+------+
1 row in set (0.00 sec)

mysql> select inet_ntoa(ip),name from jackbillow where ip=1690836502;
+---------------+------+
| inet_ntoa(ip) | name |
+---------------+------+
| 100.200.30.22 | D |
+---------------+------+
1 row in set (0.00 sec)

对于LIKE操作,可以使用下面方式:

mysql> select inet_ntoa(ip) from jackbillow;
+----------------+
| inet_ntoa(ip) |
+----------------+
| 192.168.1.200 |
| 200.100.30.241 |
| 24.89.35.27 |
| 100.200.30.22 |
| 192.168.1.100 |
| 192.168.1.20 |
| 192.168.2.20 |
+----------------+
7 rows in set (0.00 sec)

mysql> select inet_aton('192.168.1.0');
+--------------------------+
| inet_aton('192.168.1.0') |
+--------------------------+
| 3232235776 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select inet_aton('192.168.1.255');
+----------------------------+
| inet_aton('192.168.1.255') |
+----------------------------+
| 3232236031 |
+----------------------------+
1 row in set (0.00 sec)

mysql> select inet_ntoa(ip) from jackbillow where ip between 3232235776 and 3232236031;
+---------------+
| inet_ntoa(ip) |
+---------------+
| 192.168.1.200 |
| 192.168.1.100 |
| 192.168.1.20 |
+---------------+
3 rows in set (0.00 sec)

mysql> select inet_ntoa(ip) from jackbillow where ip between inet_aton('192.168.1.0') and inet_aton('192.168.1.255');
+---------------+
| inet_ntoa(ip) |
+---------------+
| 192.168.1.200 |
| 192.168.1.100 |
| 192.168.1.20 |
+---------------+
3 rows in set (0.00 sec)

标签:inet,aton,inet,ntoa
0
投稿

猜你喜欢

  • python 多线程中子线程和主线程相互通信方法

    2021-05-13 03:56:44
  • 详解python的xlwings库读写excel操作总结

    2021-03-25 02:00:00
  • python3爬虫中多线程进行解锁操作实例

    2021-04-29 00:16:42
  • ThinkPHP开发框架函数详解:C方法

    2023-10-18 03:27:04
  • Flask模板引擎Jinja2使用实例

    2022-01-19 11:58:39
  • SQL Server连接中经常出现的3个常见错误与解答

    2010-07-26 14:25:00
  • Python导入父文件夹中模块并读取当前文件夹内的资源

    2023-08-27 09:03:43
  • js特殊字符过滤的示例代码

    2023-09-07 21:53:30
  • Python实现双向链表基本操作

    2022-07-22 10:14:12
  • Python机器学习应用之支持向量机的分类预测篇

    2023-08-29 20:42:55
  • python画折线图的程序

    2021-07-04 18:08:20
  • 使用Python将数组的元素导出到变量中(unpacking)

    2022-01-12 10:11:43
  • TypeScript新语法之infer extends示例详解

    2024-03-13 20:42:27
  • 如何优化JavaScript脚本的性能

    2008-02-19 21:47:00
  • SQL Server误区30日谈 第13天 在SQL Server 2000兼容模式下不能使用DMV

    2024-01-13 18:34:25
  • Python Http请求json解析库用法解析

    2021-11-20 14:05:45
  • Python利用Turtle绘制哆啦A梦和小猪佩奇

    2022-05-08 08:38:24
  • python获取linux系统信息的三种方法

    2022-04-13 06:27:55
  • python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例

    2023-08-29 03:43:39
  • 【总结】ASP如何获取访客真实的IP地址

    2007-08-15 13:43:00
  • asp之家 网络编程 m.aspxhome.com