大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说MySQL计算两坐标距离并排序,希望您对编程的造诣更进一步.
环境
MySQL5.6
https://dev.mysql.com/doc/refman/5.6/en/spatial-relation-functions-object-shapes.html#function_st-distance
代码100分
表结构及数据
代码100分DROP TABLE IF EXISTS `locationpoint`;
CREATE TABLE `locationpoint`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`province` varchar(20) NOT NULL,
`city` varchar(20) NOT NULL,
`longitude` double(10, 3) NOT NULL,
`latitude` double(10, 3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1156
DEFAULT CHARSET = utf8;
INSERT INTO `locationpoint`
VALUES (1, "山东", "济南", 116.938477, 36.597889),
(2, "河北", "石家庄", 114.477539, 38.030786),
(3, "浙江", "杭州", 120.058594, 30.334954),
(4, "河南", "郑州", 113.629, 34.744),
(5, "安徽省", "合肥", 117.170, 31.520);
查询方式
(以内蒙古自治区呼和浩特市为计算中心)
代码100分SELECT id,
city,
longitude,
latitude,
round(
(
st_distance(
point(longitude, latitude),
point(111.621094, 40.913513)
) / 0.0111
) * 1000
)
AS distance
FROM locationpoint
ORDER BY distance;
查询结果
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/9186.html