当前位置: 移动技术网 > IT编程>数据库>Mysql > Mysql根据经纬度筛选数据

Mysql根据经纬度筛选数据

2018年11月20日  | 移动技术网IT编程  | 我要评论

创建位置表,并且插入测试数据

/*

    navicat mysql data transfer
    source server         : localhost
    source server version : 80011
    source host           : localhost:3306
    source database       : test
    target server type    : mysql
    target server version : 80011
    file encoding         : 65001
    date: 2018-11-07 16:58:27
    */
set foreign_key_checks = 0;
 
-- ----------------------------
-- table structure for location
-- ----------------------------
drop table
if exists `location`;
 
create table `location` (
    `id` int (10) unsigned not null auto_increment,
    `namevarchar (50) not null,
    `longitude` decimal (13, 10) not null,
    `latitude` decimal (13, 10) not null,
    primary key (`id`),
    key `long_lat_index` (`longitude`, `latitude`)
) engine = innodb auto_increment = 5 default charset = utf8;
 
-- ----------------------------
-- records of location
-- ----------------------------
insert into `location`
values
    (
        '1',
        '广东省深圳市龙岗区坂田街道五和大道万科四季花城北区',
        '22.6265210000',
        '114.0606880000'
    );
 
insert into `location`
values
    (
        '2',
        '广东省深圳市龙华区民治(地铁站)',
        '22.6175280000',
        '114.0406460000'
    );
 
insert into `location`
values
    (
        '3',
        '广东省深圳市龙华区红山(地铁站)',
        '22.6218860000',
        '114.0234800000'
    );
 
insert into `location`
values
    (
        '4',
        '广东省深圳市南山区西丽街道沙河西路名典商旅酒店(深圳西丽店)',
        '22.5801670000',
        '113.9543000000'
    );
 
搜索附近50km的数据
#经度:22.626521 
#纬度:114.060688
#50km范围
 
select
    *
from
    (
        select
            *, sqrt(
                (
                    (
                        (22.626521 - longitude) * pi() * 12656 * cos(
                            ((114.060688 + latitude) / 2) * pi() / 180
                        ) / 180
                    ) * (
                        (22.626521 - longitude) * pi() * 12656 * cos(
                            ((114.060688 + latitude) / 2) * pi() / 180
                        ) / 180
                    )
                ) + (
                    (
                        (114.060688 - latitude) * pi() * 12656 / 180
                    ) * (
                        (114.060688 - latitude) * pi() * 12656 / 180
                    )
                )
            as lc
        from
            location
    ) location
where
    lc < 50 ##距离小于50km
order by
    ##距离小于排序
    lc
显示结果

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网