title: What is the correct way to nest multiple SELECT statements in MySQL
body: <p>I have the following query:</p>
  
  <pre><code>SELECT 
  locations.*, 
  (SELECT COUNT(id) FROM location_scores WHERE location_id = locations.id) AS total_votes, 
  (SELECT AVG(location_score) FROM location_scores 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 &lt;= '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')
  </code></pre>
  
  <p>I keep getting the following error:</p>
  
  <blockquote>
    <p>Unknown column distance in where clause</p>
  </blockquote>