mysql使用sql语句根据经纬度计算距离排序

CREATE TABLE `locationpoint` (
  `id` int(11) NOT NULL,
  `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 DEFAULT CHARSET=utf8

INSERT INTO `locationPoint` VALUES ('1147','安徽省', '合肥', '117.170', '31.520');
INSERT INTO `locationPoint` VALUES ('1148','安徽省', '安庆', '117.020', '30.310');
INSERT INTO `locationPoint` VALUES ('1149','安徽省', '蚌埠', '117.210', '32.560');
INSERT INTO `locationPoint` VALUES ('1150','安徽省', '亳州', '115.470', '33.520');
INSERT INTO `locationPoint` VALUES ('1151','安徽省', '巢湖', '117.520', '31.360');
INSERT INTO `locationPoint` VALUES ('1152','安徽省', '滁州', '118.180', '32.180');
INSERT INTO `locationPoint` VALUES ('1153','安徽省', '阜阳', '115.480', '32.540');
INSERT INTO `locationPoint` VALUES ('1154','安徽省', '贵池', '117.280', '30.390');
INSERT INTO `locationPoint` VALUES ('1155','安徽省', '淮北', '116.470', '33.570');

  

SELECT  
    city,  
    longitude,  
    latitude,  
    ROUND(  
        6371.393 * 2 * ASIN(  
            SQRT(  
                POW(  
                    SIN(  
                        (  
                            22.53462 * 3.1415926 / 180 - latitude * PI() / 180  
                        ) / 2  
                    ),  
                    2  
                ) + COS(22.53462 * 3.1415926 / 180) * COS(latitude * PI() / 180) * POW(  
                    SIN(  
                        (  
                            113.9602 * 3.1415926 / 180 - longitude * PI() / 180  
                        ) / 2  
                    ),  
                    2  
                )  
            )  
        ) * 1000  
    ) AS distance_um  
FROM  
    locationpoint  
ORDER BY  
    distance_um ASC  

 

转载于:https://www.cnblogs.com/zyf-yxm/p/9330761.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值