Click to See Complete Forum and Search --> : two table sql


catchup
11-19-2003, 09:19 PM
I have a table called "ranges" with columns "startR" & "endR" and another table called "captured" with column "ipA"

ipA are different ip address turned into a number and startR and endR are the ranges an ISP haas, also turned into a number.

How can I select the records where ipa (from table captured) is between any of the ranges between startR and endR in the ranges table? can this be done in 1 sql? This is my ill-syntaxed effort:

"SELECT * FROM ranges WHERE (ipA FROM captured between startR AND endR)"

Thanks!

slyfox
11-20-2003, 01:51 AM
it will be best you get the ipa values from the database and calculate in your code which to display according to startr and endr

this is not final... just something to consider... maybe your idea can still be done.. but I would do it the above way

simflex
11-20-2003, 03:59 PM
First, I am surprised your code is not error-ing.
There has to be a common key between the 2 tables.
That way, you can use this code:

SELECT * FROM Ranges INNER JOIN Captured
ON Ranges.commonID = Captured.commonID
WHERE Captured.ipA BTWEEN Ranges.startR AND Ranges.endR

slyfox
11-21-2003, 05:33 AM
nice one simflex:D

CardboardHammer
11-24-2003, 01:37 AM
Or:

SELECT * FROM
Ranges INNER JOIN Captured
ON Captured.ipA >= Ranges.startR
AND Captured.ipA <=Ranges.endR


EDIT: There actually doesn't need to be ANYTHING in common between two tables to do a JOIN between them. Adding something in common just for the sake of having something in common is not a good idea.

EDIT: And if all you really want is "ipA", replace "*" with "ipA" in the code above.