I have an existing search that works fine. It is searching Table 1 (Clubs) but needs to get some data from Table 2 (Towns). To do that it does an INNER JOIN of two tables.
I am now trying to restrict the results to those within a set distance from a certain point.
Both tables have fields called latitude and longitude, but only the Towns values are populated, so I need to use those values in the search.
Several other fields use the same name in both tables, so I have to indicate which table I mean (eg. Towns.town_key).
To do the distance search I am doing a rough square box search in an inner nesting, followed by a pythagoras formula on the results of that, in an outer nesting. (I don't need the accuracy of Haversine.)
No matter what I try I keep getting errors on the fields that have the table name on the front. Unfortunately in spite of a lot of reading I do not fully understand the use of aliases etc.
Here is my search:
SELECT club_key, name, Towns.town_key, town_name, Clubs.address,
sqrt(((-32.380100 - Towns.latitude)*111.133)^2 + ((147.481400 -
Towns.longitude)*93.853)^2) AS dist FROM
(SELECT club_key, name, Towns.town_key, town_name, Clubs.address FROM Clubs
INNER JOIN Towns ON Clubs.town_key = Towns.town_key WHERE type = 'football'
AND Towns.latitude BETWEEN -32.829761 AND -31.930439 AND Towns.longitude BETWEEN 146.948951 AND 148.013849) AS T1
WHERE dist < 50 ORDER BY 6,2
The latest error is: Could not run query: Unknown column 'Towns.town_key' in 'field list'
Before that it was objecting to the latitude and longitude fields, which are used in both the inner and outer selects.