使用 SQL 检测重叠的 IP 地址范围

发布于 2024-12-12 02:40:35 字数 882 浏览 0 评论 0原文

我正在使用一个应用程序,该应用程序具有以下用于存储 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2024-12-19 02:40:36

您需要将 IP_RANGE 连接到自身,以便通过单个查询获取有关范围开始和范围结束的信息。您还需要将每次出现的 IP_RANGEIP_ADDRESS 连接起来,以便进行 IP 地址比较;所以,总而言之,你会得到这样的东西:

SELECT ip_range_start.network_id
  FROM ip_range ip_range_start
  JOIN ip_address ip_range_start_address
    ON ip_range_start.ip_id = ip_range_start_address.ip_id
  JOIN ip_range ip_range_end
    ON ip_range_start.network_id = ip_range_end.network_id
  JOIN ip_address ip_range_end_address
    ON ip_range_end.ip_id = ip_range_end_address.ip_id
 WHERE ip_range_start.range_type = 'START'
   AND ip_range_end.range_type = 'END'
   AND (    ... -- ip_range_start_address less than or equal to end of desired IP range
         OR ... -- ip_range_end_address greater than or equal to start of desired IP range
       )
;

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 of IP_RANGE to IP_ADDRESS in order to do your IP-address comparisons; so, all told, you'll have something like this:

SELECT ip_range_start.network_id
  FROM ip_range ip_range_start
  JOIN ip_address ip_range_start_address
    ON ip_range_start.ip_id = ip_range_start_address.ip_id
  JOIN ip_range ip_range_end
    ON ip_range_start.network_id = ip_range_end.network_id
  JOIN ip_address ip_range_end_address
    ON ip_range_end.ip_id = ip_range_end_address.ip_id
 WHERE ip_range_start.range_type = 'START'
   AND ip_range_end.range_type = 'END'
   AND (    ... -- ip_range_start_address less than or equal to end of desired IP range
         OR ... -- ip_range_end_address greater than or equal to start of desired IP range
       )
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文