I have the following query:
SELECT
locations.*,
(SELECT COUNT(id) FROM location_scores WHERE location_id = locations.id) AS total_votes,
(SELECT AVG(location_score) FROM location_sc开发者_StackOverflowores WHERE location_id = locations.id) AS rating,
(SELECT COUNT(id) FROM location_views WHERE location_id = locations.id) AS total_views,
(SELECT COUNT(id) FROM location_procedures WHERE location_id = locations.id) AS total_procedures,
(SELECT ((ACOS(SIN(32.9063840 * PI() / 180) * SIN(location_latitude * PI() / 180) + COS(32.9063840 * PI() / 180) * COS(location_latitude * PI() / 180) * COS((-96.8590890 - location_longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) FROM locations) AS distance
FROM locations
WHERE distance <= '5'
AND locations.id IN ('57', '57', '57', '57', '57', '57', '57', '57', '57', '57', '68', '68', '70', '73', '73', '76', '76', '76', '76', '76', '77', '77')
I keep getting the following error:
Unknown column distance in where clause
distance
is the name of a table, not a column.
Put an AS column_name
after that last long subquery, then you would access it with something like.
WHERE distance.column_name <= 5
The distance seems to be a table. The simplified query:
SELECT (... FROM locations) AS distance
Maybe, you forget the where clase:
SELECT (... FROM locations WHERE location_id = locations.id) AS distance
精彩评论