I have a site and am allowing users to search for other users within a specified radius of their own zip code. I first have to locate the latitude and longitude of the user and use that data to calculate and find other users. I've prepared the following query based on the 2 required tables but am getting errors that another alias is needed. If I hard code the values then parts of the query work find when I split them out, but with multiple joins and subqueries I've been unsuccessful. Looking for any guidance or help. Query and table structures included below.
Edited query and data set based on feedback. No longer receiving errors but am not getting any data returned for the query.
SQL Statement
SELECT u.username, u.zip, m.longitude,
m.latitude,
m.zip from users u, zipcodes m
LEFT JOIN (SELECT s.zip,
s.username from users s) t
on m.zip = t.zip
and t.username = 'tester55'
INNER join ( SELECT zip, city, state, ( 3959 * acos( cos( radians(@m.latitude) ) * cos( radians( latitude ) )
* cos( radians( longitude ) - radians(@m.longitude) ) + sin( radians(@m.latitude) ) * sin(radians(latitude)) ) ) AS distance
FROM zipcodes
HAVING distance < 5000
ORDER BY distance) as RelevantCodes
ON (t.zip=RelevantCodes.zip)
User Table
username zip
-----------------------
tester55 60608
Tester1 60608
iosuser1 73120
Tester3 60608
Zip Code Table
latitude longitude zip
--------------------------------
41.8515 -87.6694 60608
35.5835 -97.5638 73120
36.3169 -94.1545 72758
I don't have the time right now to work out a correct query, but I'll show what is wrong with the existing query at a first glance.
1) Regarding your actual question: The error message reads
ERROR 1248 (42000): Every derived table must have its own alias
and already tells what is wrong. The subquery in the following partneeds its own alias, so that should eventually be
(note the t after the closing parenthesis and the replacement of s.zip by t.zip). But even if you do that, you will run into problem 2 (see next section).
2) The subquery
will throw another error because the table m is not known within that subquery.
If you are able to work it out correctly, I suggest you edit your question and let us know the result. If not, please post a comment or edit your question, describing what other problems you encounter.
EDIT
I now have worked out a solution for you:
This selects all ZIP codes which are within the desired distance, ordered by distance.
As you may notice, I have simplified the code. Please note also that I have tested this based on your old data (the data you have shown before your edit), but using
Tester1
instead oftester55
.You indeed need the
HAVING
instead ofWHERE
when filtering, so my initial statement regarding that was misleading. The reason is that MySQL does not allow computed columns (in this case: distance) inWHERE
clauses, but inHAVING
clauses. This is non-standard behavior; usually, you have to solve this problem another way. I won't elaborate on that because it is an additional possibly worrying thing which is not closely related to your original question.EDIT 2
Finally, here is the version which gives the username in addition:
Please note that you need the third from last line to filter out the user you are calculating the distances for. If you don't do that, the result will include a row with the user itself (in this case:
Tester1
) and a distance of (nearly)0
.EDIT 3
The following is a version as requested by the comments below (desired seeking radius stored as additional field
seekingdistance
in tableusers
):