Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5844069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:14:32+00:00 2026-05-22T12:14:32+00:00

My SQL query is working fine, until I try to add a ‘WHERE distance

  • 0

My SQL query is working fine, until I try to add a ‘WHERE distance < 10’ and ‘chunk-of-calculation AS distance’ on 4th and 10th line respectively. Any idea how I can fix it? Thanks!

Unknown column 'distance' in 'where clause'

SELECT SQL_CALC_FOUND_ROWS places.*, category.*, 
COUNT(places_reviews.place_id) AS num_reviews, 
(places_popularity.rating_1 + 2*places_popularity.rating_2 + 3*places_popularity.rating_3 + 4*places_popularity.rating_4 + 5*places_popularity.rating_5)/(places_popularity.rating_1 + places_popularity.rating_2 + places_popularity.rating_3 + places_popularity.rating_4 + places_popularity.rating_5) AS average_rating, 
6371 * acos( cos( radians(places.lat) ) * cos( radians( 1.29315 ) ) * cos( radians( 103.827164 ) - radians(places.lng) ) + sin( radians(places.lat) ) * sin( radians( 1.29315 ) ) ) AS distance 
FROM (places) 
JOIN category 
ON places.category_id = category.category_id 
LEFT JOIN places_reviews ON places_reviews.place_id = places.id 
LEFT JOIN places_popularity ON places_popularity.place_id = places.id 
WHERE `places`.`category_id` = 1 AND `distance` < 5 AND places.name LIKE '%%' GROUP 
BY places.id 
ORDER BY id desc 
LIMIT 5
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:14:32+00:00Added an answer on May 22, 2026 at 12:14 pm

    You will need to put the formula in your WHERE clause instead of using the alias distance. In a SQL query, the WHERE clause is evaluated before the SELECT statement so the alias (in this case distance) does not exist yet. Here is what your SQL statement will look like:

    SELECT SQL_CALC_FOUND_ROWS places.*, category.*, 
    COUNT(places_reviews.place_id) AS num_reviews, 
    (places_popularity.rating_1 + 2*places_popularity.rating_2 + 3*places_popularity.rating_3 + 4*places_popularity.rating_4 + 5*places_popularity.rating_5)/(places_popularity.rating_1 + places_popularity.rating_2 + places_popularity.rating_3 + places_popularity.rating_4 + places_popularity.rating_5) AS average_rating, 
    6371 * acos( cos( radians(places.lat) ) * cos( radians( 1.29315 ) ) * cos( radians( 103.827164 ) - radians(places.lng) ) + sin( radians(places.lat) ) * sin( radians( 1.29315 ) ) ) AS distance 
    FROM (places) 
    JOIN category 
    ON places.category_id = category.category_id 
    LEFT JOIN places_reviews ON places_reviews.place_id = places.id 
    LEFT JOIN places_popularity ON places_popularity.place_id = places.id 
    WHERE `places`.`category_id` = 1 
       AND (6371 * acos( cos( radians(places.lat) ) * cos( radians( 1.29315 ) ) * cos( radians( 103.827164 ) - radians(places.lng) ) + sin( radians(places.lat) ) * sin( radians( 1.29315 ) ) )) < 5
       AND places.name LIKE '%%' GROUP 
    BY places.id 
    ORDER BY id desc 
    LIMIT 5
    

    The only way you could refer to distance by name would be to wrap your statement and make it into a table in a new SELECT statement. For example:

    SELECT *
    FROM ( <insert your original query here without the WHERE distance= statement ) AS t
    WHERE distance < 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq to SQL query that was working just fine with SQL
I have a SQl query working fine, I need to convert it to LINQ
I am stuck figuring out a working SQL Query for the fallowing: I need
I'm very interesting in inexpensive (better free) SQL query tool for working with multiple
I am working on a SQL query that reads from a SQLServer database to
I'm working on creating a SQL query that will pull records from a table
I have SQL query, which is working nice on Oracle and MSSQL. Now I'm
I am working on .aspx page that uses a t-sql query that uses a
I am currently working with 2 sql 2000 servers one I can query but
In a SQL Server 2005 database I'm working on this query: select * from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.