0

我有一个包含几千个职位的数据库。这些是公共汽车站。我想要做的是获得最接近 LON/LAT 位置的 X 个值。

这是我的桌子的布局:

CREATE TABLE IF NOT EXISTS `buss_StopPoints` (
  `Name` varchar(50) NOT NULL,
  `LocationNorthing` varchar(30) NOT NULL,
  `LocationEasting` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这些是一些示例行:

INSERT INTO `buss_StopPoints` (`Name`, `LocationNorthing`, `LocationEasting`) VALUES ('Some station', '59.3195952318672', '18.0717401337168');
INSERT INTO `buss_StopPoints` (`Name`, `LocationNorthing`, `LocationEasting`) VALUES ('Some station 2', '59.3195772927918', '18.0717389396547');
INSERT INTO `buss_StopPoints` (`Name`, `LocationNorthing`, `LocationEasting`) VALUES ('Some station 3', '59.3234014331742', '18.0671617033088');
INSERT INTO `buss_StopPoints` (`Name`, `LocationNorthing`, `LocationEasting`) VALUES ('Some station 4', '59.3233921590479', '18.0671786573678');
INSERT INTO `buss_StopPoints` (`Name`, `LocationNorthing`, `LocationEasting`) VALUES ('Some station 5', '59.3313179695727', '18.061677395945');

我的问题:如何创建一个 select 语句,从 DB 中选择最近的 X 行,无论是在负方向还是正方向。(例如,假设我要求 59.3234014331742,18.0671617033088 并想要最近的 4 个站,在这种情况下应该全部返回)。

4

1 回答 1

2
LAT = latitude value
LON = longitude value

SELECT Name, (6371 * acos( cos( radians(LAT) ) * cos( radians( LocationNorthing ) ) * cos( radians( LON ) - radians(LocationEasting) ) + sin( radians(LAT) ) * sin( radians(LocationNorthing) ) )) AS distance order by distance
于 2012-06-03T17:46:20.083 回答