使用 SQL 检测重叠的 IP 地址范围
我正在使用一个应用程序,该应用程序具有以下用于存储 IP 地址范围的表:
IP_RANGE
--------
RANGE_ID primary key
NETWORK_ID foreign key into NETWORK table (not described here)
IP_ID foreign key into IP_ADDRESS table
RANGE_TYPE varchar, values "START" or "END"
IP_ADDRESS
----
IP_ID primary key
IP_NETWORK number (decimal representation of network portion of address)
IP_INTERFACE number (decimal representation of interface portion of address)
给定的 IP 地址范围由 IP_RANGE 表中的两行表示:一行的 RANGE_TYPE 值为“START”,另一行的值为“END” ",两者的 NETWORK_ID 值相同。这些行中的每一行还指向 IP_ADDRESS 表中存储实际地址的行。
我需要编写一个 SELECT 语句,为我提供所有 NETWORK_ID,这些 NETWORK_ID 的 IP 范围具有与给定任意 IP 范围相同的任何地址。
我知道如何检查 IP_ADDRESS 表以查找位于所需范围内或之外的各个地址,但我不知道如何将搜索限制为仅具有相同 NETWORK_ID 的开始/结束对。
(我想这实际上是一个纯粹的 SQL 问题,而不是任何专门与 IP 地址相关的问题。)
Oracle Database 11g 企业版版本 11.1.0.7.0 - 64 位生产
I'm working with an application that has the following tables for storing IP address ranges:
IP_RANGE
--------
RANGE_ID primary key
NETWORK_ID foreign key into NETWORK table (not described here)
IP_ID foreign key into IP_ADDRESS table
RANGE_TYPE varchar, values "START" or "END"
IP_ADDRESS
----
IP_ID primary key
IP_NETWORK number (decimal representation of network portion of address)
IP_INTERFACE number (decimal representation of interface portion of address)
A given IP address range is represented by two rows in the IP_RANGE table: one with a RANGE_TYPE value of "START" and another with a value of "END", both of which have the same value for NETWORK_ID. Each of these rows also point to a row in the IP_ADDRESS table which stores the actual addresses.
I need to write a SELECT statement that gives me all of the NETWORK_IDs which have IP ranges that have any addresses in common with a given arbitrary IP range.
I know how to inspect the IP_ADDRESS table to find individual addresses that fall inside or outside my desired range, but I don't know how to restrict my search to only begin/end pairs that have the same NETWORK_ID.
(I suppose this is really a pure SQL question rather than anything specifically to do with IP addresses.)
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
IP_RANGE
连接到自身,以便通过单个查询获取有关范围开始和范围结束的信息。您还需要将每次出现的IP_RANGE
与IP_ADDRESS
连接起来,以便进行 IP 地址比较;所以,总而言之,你会得到这样的东西:You need to join
IP_RANGE
to itself to get information about both the range-start and the range-end with a single query. You also need to join each occurrence ofIP_RANGE
toIP_ADDRESS
in order to do your IP-address comparisons; so, all told, you'll have something like this: