views:

86

answers:

1

I'm running this query

SELECT 
country, 
countries.code, 
countries.lat, 
countries.lng, 
countries.zoom, 
worldip.start, 
worldip.end 

FROM countries, worldip 
WHERE countries.code = worldip.code 
AND 
'91.113.120.5' BETWEEN worldip.start AND worldip.end 
ORDER BY worldip.start DESC

on a tables with these fields,

worldip           countries
--------------    ----------------
start             code
end               country
code              lat
country_name      lng
                  zoom

And sometimes I'm getting two results in two different countries for one ip. I understand why

'91.113.120.5' BETWEEN worldip.start AND worldip.end

would return two different results since 10 is between 9 and 11, but also 5 and 12. I would have thought including WHERE countries.code = worldip.code would have prevented this, or at least ensure I got the right country no matter how many results it returned. but it doesn't.

I also added ORDER BY worldip.start DESC which seems to work since the more accurate an ip adress, the higher up the list it appears. you can see it working (or not) here . But that's a quick fix and I'd like to do it right.

SQL is a real weak point for me. Can anyone explain what I'm doing wrong?

+2  A: 

Firstly nice app. I was looking for flights - I would love price comparisons and no #based links please. You could try a free geolocator service instead of using your own geoip database. That aside is your ip field of 'IP' datatype in MySQL allowing comparison ? This may help you get correct ordinality. Otherwise the stuff is compared as strings and problems may arise where the length of IP's is different and so on.

With integer representation of IP's you can use the <= and >= operators.

whatnick
Thanks for the answer and feedback. I'm going over everything and taking out all the dirty little shortcuts I cheated with (href="#"!! I know better). Alas, getting the prices for flights is, at the moment, out of reach. That info costs a lot of money, I'd have to pay per query. The fees are extortionate. But if anyone wants to invest, then I'm all ears. But there's more stuff coming for free, connecting flights, hotels, special offers etc.
gargantaun
I guess that's where flight agents make the money but can't you get prices off virgin or jetstar or tiger (I am in Aus) by script submitting their query forms and scrubbing the HTML ?
whatnick
yup, that's an idea I've been toying with, but with 2000 airlines to be going at, that's a lot of coding. Although I may just have to start it, unless you want to get in on the action?
gargantaun