SQL Query for Performing Radius Search based on Latitude Longitude

前端 未结 4 879
天命终不由人
天命终不由人 2020-12-04 10:31

We have a restaurant table that has lat-long data for each row.

We need to write a query that performs a search to find all restaurants within the prov

4条回答
  •  盖世英雄少女心
    2020-12-04 11:14

    You may want to create a SPATIAL index on your table to make the searches faster.

    To do this, add a POINT column to your table:

    ALTER TABLE restaurant ADD coords POINT NOT NULL;
    
    CREATE SPATIAL INDEX sx_restaurant_coords ON restaurant (coords);
    
    SELECT  *
    FROM    restaurant
    WHERE   MBRContains(coords, LineString(Point(583734 - 1609, 4507223 - 1609), Point(583734 + 1609, 4507223 + 1609))
            AND GLength(LineString(Point(583734, 4507223), coords)) <= 1609
    

    You should store coords as UTM coordinates within a single zone.

提交回复
热议问题