首页 > 数据库 > mysql教程 > MySQL对IP地址进行排序

MySQL对IP地址进行排序

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-07 17:30:43
原创
1499 人浏览过

利用mysql自带内置函数: INET_ATON(expr)Given the dotted-quad representation of a network address as a string, returns an

利用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执行计划改变

MySQL数据库安全模式登陆

linux

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
排序怎么没有啊!!
来自于 1970-01-01 08:00:00
0
0
0
多列升序排序的MySQL实现方法
来自于 1970-01-01 08:00:00
0
0
0
MySQL: 递增排序重复的数字
来自于 1970-01-01 08:00:00
0
0
0
全局排序
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板