Skip to main content

Joining table on a range

  • November 2, 2017
  • 2 replies
  • 16 views

DBDave
Forum|alt.badge.img
  • Participating Frequently

Hello,

I have a rather rare issue. I have 2 tables. Let's call them IP and ICP. These tables hold IP addresses. I need to join these 2 tables, but my only join condition is a range of IP's.

Let me explain...

In table IP we have a column called IPAddress. It's self explanatory what this column contains. In the ICP table there are 2 columns... IPAddress_start and IPAddress_end. So, I need to join the IPAddress from the IP table to the range between IPAddress_start and IPAddress end.

I have tried the following that hasn't worked. I also tried SUBQUERIES, but the query returns more than 1 row so I get an error message.

Any help would be greatly appreciated.

SELECT * FROM IP
JOIN ICP
ON IP.IPAddress >= ICP.IPAddress_start
OR IP.IPAddress <= ICP.IPAddress_end;

SELECT * FROM IP
WHERE EXISTS (SELECT * FROM ICP WHERE
IP.IPAddress >= ICP.IPAddress_start OR
IP.IPAddress <= ICP.IPAddress_end);

2 replies

skeswani
Forum|alt.badge.img
  • Participating Frequently
  • November 2, 2017

DBDave
Forum|alt.badge.img
  • Author
  • Participating Frequently
  • November 3, 2017

Thank you. The BETWEEN worked. I didn't think it would :)