将 IP 地址与 IP 范围匹配?
我有一个 MySQL 表设置如下:
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| ipaddress_s | varchar(15) | YES | MUL | NULL | |
| ipaddress_e | varchar(16) | YES | | NULL | |
+---------------+-------------+------+-----+---------+----------------+
其中,ipaddress_s 和 ipaddress_e 看起来像: 4.100.159.0-4.100.159.255
现在有没有一种方法可以真正获取包含给定 IP 地址的行?例如,给定 IP 地址:“4.100.159.5”,我希望返回上面的行。所以我正在尝试一个看起来像这样的查询(但这当然是错误的,因为在下面我将 IP 视为字符串):
SELECT * FROM ranges WHERE ipaddress_s<"4.100.159.5" AND ipaddress_e>"4.100.159.5"
有什么建议吗?
I have a MySQL table setup as follows:
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| ipaddress_s | varchar(15) | YES | MUL | NULL | |
| ipaddress_e | varchar(16) | YES | | NULL | |
+---------------+-------------+------+-----+---------+----------------+
where, ipaddress_s and ipaddress_e look something like: 4.100.159.0-4.100.159.255
Now is there a way I can actually get the row that contains a given IP address? For instance, given the IP address: "4.100.159.5", I want the above row to be returned. So I am trying for a query that looks something like this (but of course this is wrong because in the following I am considering IPs as strings):
SELECT * FROM ranges WHERE ipaddress_s<"4.100.159.5" AND ipaddress_e>"4.100.159.5"
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
INET_ATON(expr) 会将 IP 地址字符串更改为可用于比较的数字地址。
http://dev.mysql.com/doc /refman/5.0/en/miscellaneous-functions.html#function_inet-aton
INET_ATON(expr) will change an ip address string into a numeric address that you can use for comparison.
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton