ThinkPHP+MySQL查询数据的时候计算两个经纬度之间的距离并根据距离进行筛选

原需求实现说明

新增了一个按距离进行筛选的需求。需要把查询代码做如下修改

php 复制代码
    /**
     * 求职意向
     * @return void
     * @throws \think\exception\DbException
     */
    public function get_lists(){
        $request = $this->request->get();
        if(empty($request['lng']) || empty($request['lat'])){
            $this->error('位置信息错误');
        }
        $where = [];
        //筛选城市
        if(!empty($request['city'])){
            $where['p.city'] = array('eq',$request['city']);
        }
        //筛选姓名或者岗位名
        if(!empty($request['keywords'])){
            $where['r.name|c.name'] = array('like','%'.$request['keywords'].'%');
        }
        //筛选工种
        if(!empty($request['gongzhong'])){
            $where['p.gongzhong'] = array('eq',$request['gongzhong']);
        }
        //筛选性别
        if(isset($request['sex']) && in_array($request['sex'],[0,1])){
            $where['r.sex'] = array('eq',$request['sex']);
        }

        //筛选距离
        $sql = '';
        if(!empty($request['distance'])){
            $sql = "ROUND(
                    6378.138 * 2 * ASIN(
                        SQRT(
                            POW(SIN((".$request['lat']." * PI() / 180 - p.lat * PI() / 180) / 2), 2) +
                            COS(".$request['lat']." * PI() / 180) * COS(p.lat * PI() / 180) * POW(SIN((".$request['lng']." * PI() / 180 - p.lng * PI() / 180) / 2), 2)
                        )
                    ) * 1000
                ) < ".$request['distance'];
        }
        //最高学历筛选
        if(!empty($request['zuigaoxueli'])){
            $where['r.zuigaoxueli'] = array('eq',$request['zuigaoxueli']);
        }
        $lists = PurposeModel::alias('p')
            ->join('user u','p.user_id = u.id')
            ->join('resume r','p.user_id = r.user_id')
            ->join('category c','p.category_id = c.id')
            //根据生日计算年领(YEAR(CURDATE()) - YEAR(birthday)) AS age 根据经纬度计算距离
            ->field('p.user_id,r.name,u.avatar,(YEAR(CURDATE()) - YEAR(birthday)) AS age,r.sex,r.zuigaoxueli,c.name as category_name,p.gongzhong,p.work_begin,p.work_end,p.rest_begint,p.rest_end,p.city,p.address,'.getDistanceBuilder($request['lat'], $request['lng']))
            ->where($where)
            ->where($sql)
            ->order('distance asc')
            ->paginate();
        $this->success('获取成功',$lists);
    }

得到的SQL如下

sql 复制代码
SELECT 
    p.user_id,
    r.name,
    u.avatar,
    (YEAR(CURDATE()) - YEAR(birthday)) AS age,
    r.sex,
    c.name AS category_name,
    p.gongzhong,
    p.work_begin,
    p.work_end,
    p.rest_begint,
    p.rest_end,
    p.city,
    p.address,
    ROUND(
        6378.138 * 2 * ASIN(
            SQRT(
                POW(SIN((34.781089 * PI() / 180 - p.lat * PI() / 180) / 2), 2) +
                COS(34.781089 * PI() / 180) * COS(p.lat * PI() / 180) * POW(SIN((113.61261 * PI() / 180 - p.lng * PI() / 180) / 2), 2)
            )
        ) * 1000
    ) AS distance
FROM 
    `fa_purpose` `p`
INNER JOIN 
    `fa_user` `u` ON `p`.`user_id` = `u`.`id`
INNER JOIN 
    `fa_resume` `r` ON `p`.`user_id` = `r`.`user_id`
INNER JOIN 
    `fa_category` `c` ON `p`.`category_id` = `c`.`id`
WHERE 
    `p`.`city` = '郑州'  
    AND (`r`.`name` LIKE '%无人机%' OR `c`.`name` LIKE '%无人机%')
    AND `p`.`gongzhong` = '1'
    AND `r`.`sex` = '0'
    AND ROUND(
        6378.138 * 2 * ASIN(
            SQRT(
                POW(SIN((34.781089 * PI() / 180 - p.lat * PI() / 180) / 2), 2) +
                COS(34.781089 * PI() / 180) * COS(p.lat * PI() / 180) * POW(SIN((113.61261 * PI() / 180 - p.lng * PI() / 180) / 2), 2)
            )
        ) * 1000
    ) < 500
ORDER BY 
    distance ASC
相关推荐
不太可爱的叶某人5 小时前
【学习笔记】MySQL技术内幕InnoDB存储引擎——第5章 索引与算法
笔记·学习·mysql
量子联盟6 小时前
原创-基于 PHP 和 MySQL 的证书管理系统,免费开源
开发语言·mysql·php
飞翔的佩奇7 小时前
Java项目:基于SSM框架实现的旅游协会管理系统【ssm+B/S架构+源码+数据库+毕业论文】
java·数据库·mysql·毕业设计·ssm·旅游·jsp
鬼才血脉8 小时前
Linux(centos)安装 MySQL 8
linux·mysql·centos
眠りたいです16 小时前
Mysql常用内置函数,复合查询及内外连接
linux·数据库·c++·mysql
M1A117 小时前
Java 面试系列第一弹:基础问题大盘点
java·后端·mysql
He.ZaoCha17 小时前
函数-1-字符串函数
数据库·sql·mysql
叁沐18 小时前
MySQL 09 普通索引和唯一索引,应该怎么选择?
mysql
Linda L19 小时前
数据库版本自动管理
运维·数据库·mysql
草履虫建模19 小时前
Redis:高性能内存数据库与缓存利器
java·数据库·spring boot·redis·分布式·mysql·缓存