everyone:
I am completely stuck with this problem. I have tried adding conditions as a WHERE statement, IF function in the SELECT statement, and ON statement when joining. None of these produce the result I want.
WHAT I NEED: A list of cities within 0.5 degrees latitude and 0.5 degrees longitude of a specific location.
WHAT I WAS GIVEN: Two tables - One table with a list of specific locations and another table with a list of cities as stated by US Census.
Table 1 includes:
Location Address
City
State
Location Latitude
Location Longitude
Sample Location Data:
Street City State Latitude Longitude
774 Emerson Street Palo Alto CA 37.44239044 -122.15956879
Table 2, which is US Census data, includes:
City
State
City Latitude
City Longitude
Sample City Data:
City State Latitude Longitude
Palo Alto CA 37.44188309 -122.14302063
Mountain View CA 37.38605118 -122.08385468
Sunnyvale CA 37.36883 -122.0363496
So for example, if I have a location in Palo Alto, CA, I want something that looks like this:
Location City State
Palo Alto CA Palo Alto CA
Palo Alto CA Mountain View CA
Palo Alto CA Sunnvyale CA
I tried:
SELECT d.`City`
,d.`State`
,l.`City`
,l.`StateAbbreviation`
FROM `LocationDirectoryRevised` AS d
LEFT JOIN
`LatLong` AS l
ON d.`City` = l.`City` AND d.`State` = l.`StateAbbreviation`
WHERE l.`Latitude` BETWEEN (d.`Latitude` + 0.5) AND (d.`Latitude` - 0.5)
AND
l.`Longitude` BETWEEN (d.`Longitude` + 0.5) AND (d.`Longitude` - 0.5);
I tried:
SELECT d.`City`
,d.`State`
,l.`City`
,l.`StateAbbreviation`
FROM `LocationDirectoryRevised` AS d
LEFT JOIN
`LatLong` AS l
ON l.`Latitude` BETWEEN (d.`Latitude` + 0.5) AND (d.`Latitude` - 0.5) &&
l.`Longitude` BETWEEN (d.`Longitude` + 0.5) AND (d.`Longitude` - 0.5)
I tried:
SELECT d.`City`
,d.`State`
,IF((l.`Latitude` < (d.`Latitude` + 0.5)) && (l.`Latitude` > (d.`Latitude` - 0.5)) && (l.`Longitude` < (d.`Longitude` + 0.5)) && (l.`Longitude` > (d.`Longitude` - 0.5)), l.`City`,NULL)
,l.`StateAbbreviation`
FROM `LocationDirectoryRevised` AS d
LEFT JOIN
`LatLong` AS l
ON d.`City` = l.`City` AND d.`State` = l.`StateAbbreviation`
But these did not produce the results I wanted. What am I missing?
Thanks.