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
You need to join
IP_RANGEto 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_RANGEtoIP_ADDRESSin order to do your IP-address comparisons; so, all told, you’ll have something like this: