Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

mysql - Unable to run the sql query


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

MySQL, along with most other flavors of SQL, does not allow referring to an alias in the WHERE clause defined in a select at the same level of the query. So, your WHERE clause is invalid. Fortunately MySQL has overloaded its HAVING operator to allow referring to an alias, so you may try that:

SELECT * , ACOS( SIN( RADIANS( lat )) * SIN( RADIANS( 37.387138 )) + 
    COS( RADIANS( lat )) * COS( RADIANS( 37.387138 )) * COS( RADIANS( lng ) 
    - RADIANS( -122.083237 )) ) * 6380 AS distance 
FROM restaurants
HAVING distance < 10
ORDER BY distance;

Also note that your restaurants table, and lat/lng columns, should not be in single quotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...