1

Is this query working fine or not ?? any one help to execute this query, i want 5km surrounding values when i giving curent latittide and longitude

SELECT
    id, (
      3959 * acos (
      cos ( radians(12.966958) )
      * cos( radians( latitude ) )
      * cos( radians( longitude ) - radians(80.1525835) )
      + sin ( radians(12.966958) )
      * sin( radians( latitude ) )
    )
) AS distance
FROM place
HAVING distance < 5 ORDER BY distance
Shadow
  • 33,525
  • 10
  • 51
  • 64
user7346035
  • 381
  • 2
  • 6
  • 11

1 Answers1

1

HAVING is used only when you want to filter results after an aggregation function - You have to use WHERE here.

Also,

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

SELECT id, distance FROM(
SELECT
    id, (
      3959 * acos (
      cos ( radians(12.966958) )
      * cos( radians( latitude ) )
      * cos( radians( longitude ) - radians(80.1525835) )
      + sin ( radians(12.966958) )
      * sin( radians( latitude ) )
    )
) AS distance
FROM place) tmp
WHERE tmp.distance >5
ORDER by distance
Karan Shah
  • 1,304
  • 11
  • 15