I use MySQL Spatial Extensions to store data about roads and hotels. I store hotel data as a point, while I store road data as LineString. The tables look like this:
CREATE TABLE IF NOT EXISTS `Hotels` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `name` text, `coordinate` point NOT NULL, PRIMARY KEY (`id`), SPATIAL KEY `coordinate` (`coordinate`), ) CREATE TABLE IF NOT EXISTS `Roads` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `name` text, `route` linestring NOT NULL, PRIMARY KEY (`id`), SPATIAL KEY `coordinate` (`route`), )
Visualization of the instance will be like this.

My problem is given the number N and the point P, what is the SQL query to find the N nearest roads from point P? The distance is determined by the smallest perpendicular distance between the length of the road to the point as shown above. (although in reality the closest distance should be between the highway gate and the hotel, but in this case we can enter the highway from anywhere: P)
If for this problem there is no single solution to the SQL query, intermediate SQL query and post processing are acceptable to me. But what will be the effective SQL query and how to perform post-processing of data?
mysql geospatial
John hancock
source share