开发者

What is the correct way to nest multiple SELECT statements in MySQL

开发者 https://www.devze.com 2023-03-28 08:43 出处:网络
I have the following query: SELECT locations.*, (SELECT COUNT(id) FROM location_scores WHERE location_id = locations.id) AS total_votes,

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
0

精彩评论

暂无评论...
验证码 换一张
取 消