手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL对IP地址进行排序

[日期:2013-07-23] 来源:Linux社区  作者:jemary [字体: ]

利用MySQL自带内置函数:

INET_ATON(expr)
Given the dotted-quad representation of a network address as a string, returns an integer that represents the numeric value of the address. Addresses may be 4- or 8-byte addresses.
mysql> SELECT INET_ATON('209.207.224.40');
      -> 3520061480
The generated number is always in network byte order. For the example just shown, the number is calculated as 209×2563 + 207×2562 + 224×256 + 40.
INET_ATON() also understands short-formIPaddresses:
mysql> SELECT INET_ATON('127.0.0.1'), INET_ATON('127.1');
      -> 2130706433, 2130706433
Note: When storing values generated by INET_ATON(), it is recommended that you use an INT UNSIGNED column. If you use a (signed) INT column, values corresponding toIPaddresses for which the first octet is greater than 127 cannot be stored correctly. See Section 11.2, “Numeric Types”.

INET_NTOA(expr)
Given a numeric network address (4 or 8 byte), returns the dotted-quad representation of the address as a string.
mysql> SELECT INET_NTOA(3520061480);
      -> '209.207.224.40'

以上是Mysql手册中的说明,实际使用方法:

我的ip是以字符串形式存储在表table_ip的ip_info字段中,可以用下面的查询语句返回以ip排序的结果
select ip_info from table_ip order by inet_aton(ip_info);
结果:
192.168.6.10
192.168.6.60
92.168.6.120
92.168.6.240
...

推荐阅读:

数据量增加导致MySQL执行计划改变 http://www.linuxidc.com/Linux/2013-07/87592.htm

MySQL数据库安全模式登陆 http://www.linuxidc.com/Linux/2013-07/87601.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款