• 写yi一个mysql存储过程


     1 //存储过程,利用已知的经纬度查询其他帖子距离自己的距离。juli的算法是网上拿的
     2             //如果存在就删除这个存储过程
     3             >>     drop procedure if exists `get_distance`; 
     4             //设置默认结束符为%%
     5             >>     delimiter %%
     6             //开始创建语句,4个参数,in 表示输入值,可以省略;格式: in 变量名 类型
     7             >>     create procedure get_distance (in lng_put decimal(12,8),in lat_put decimal(12,8),in where_str varchar(255),in limit_str varchar(100))
     8             //开始创建存储过程内容
     9                 begin
    10             //if 判断,注意括号内为一个等号。。还有then
    11                 if(limit_str = '') then
    12                     set @limit = '';
    13                 else
    14                     set @limit = concat(' ',limit_str);
    15             //end if 后面要加分好
    16                 end if;
    17                 if(where_str = '') then
    18                     set @where = '';
    19                 else 
    20                     set @where = concat(' ',where_str,' ');
    21                 end if;
    22             //拼接sql语句
    23                 set @sql_str     = concat('select *,ROUND(6378.138*2*ASIN(SQRT(POW(SIN((',lat_put,'*PI()/180-latitude*PI()/180)/2),2)+COS(',lat_put,'*PI()/180)*COS(latitude*PI()/180)*POW(SIN((',lng_put,'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli FROM gd_bbs_article ',@where,'ORDER BY juli asc',@limit);
    24             //准备sql语句
    25                 prepare temp from @sql_str;
    26             //执行sql语句
    27                 execute temp;
    28             //结束
    29                 end%%
    30             //还原
    31             >> delimiter ;
    32             //删除此存储过程
    33             >> drop procedure get_distance%%
    34 
    35             //查看
    36             show procedure like '%name%'

    调用过程

    1 $sql         = 'call get_distance(112.075387,24.053732,"","limit 0,10")';
    2 $res         = mysql_query($sql);
    3 //var_dump($res . mysql_error());
    4 while(($row = mysql_fetch_assoc($res)) !== false) {
    5     print_r($row);
    6 }

    函数

    delimiter %%
    create function get_lnglat_distance(
    lng_current decimal(12,8),
    lat_current decimal(12,8),
    lng_db decimal(12,8),
    lat_db decimal(12,8)
    )
    returns int//这里是returns 
    begin
    declare distance int;//declare 局部
    set distance = ROUND(6378.138*2*ASIN(SQRT(POW(SIN((lat_current*PI()/180-lat_db*PI()/180)/2),2)+COS(lat_current*PI()/180)*COS(lat_db*PI()/180)*POW(SIN((lng_current*PI()/180-lng_db*PI()/180)/2),2)))*1000);
    return distance;
    end
    %%
    delimiter ;

    调用

    select article_id,get_lnglat_distance(112.075387,24.053732,longitude,latitude) juli from gd_bbs_article order by juli asc limit 0,20;
  • 相关阅读:
    2021年下半年北京市中小学教师资格考试笔试报名公告
    高效演讲
    php的Allowed memory size of 134217728 bytes exhausted问题解决办法
    1111error
    http 500 错误
    xshell连接centons
    Vue 计算属性
    Vue 自定义指令
    Vue 事件绑定
    Vue v-cloak指令解决插值表达式“闪动”问题
  • 原文地址:https://www.cnblogs.com/lxdd/p/4234379.html
Copyright © 2020-2023  润新知