MySQL query returning errors
I'm trying to get coordinates and locations from a database, but my server is returning this error:
Error: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'long ) - radians(0) ) + sin( radians(0) ) * sin( radians( lat ) ) ) ) AS distanc'
at line 1
Query:
SELECT id,
address,
name,
(3959 * acos(cos(radians(0)) *
cos(radians(lat))开发者_运维技巧 *
cos(radians(long) - radians(0)) +
sin(radians(0)) *
sin(radians(lat)))
) AS distance
FROM places
HAVING distance < 10
ORDER BY distance
LIMIT 0, 20;
Do I have to escape "lat" and "long" with `, or is that formula wrong altogether? Thanks.
Also, the coordinates I'm using are 0, 0 (just for test purposes).
LONG is a reserved word in mysql. Escape it using backticks when using it as a column name.
Replace the HAVING statement with "WHERE" statement. There is no "GROUP BY", then why are you using HAVING?
I think you have an extra )
. Try this
SELECT id, address, name, ( 3959 * acos( cos( radians(0) ) * cos( radians( lat ) ) * cos( radians( long ) - radians(0) ) + sin( radians(0) ) * sin( radians( lat ) ) ) AS distance FROM places HAVING distance < 10 ORDER BY distance LIMIT 0 , 20;
精彩评论