• mysql 巧用存储过程


    根据距离排序

    CREATE DEFINER=`ln` PROCEDURE `Proc_4`(IN `lon1` double,IN `lat1` double,IN `PageStart` int,IN `PageStep` int,IN `type` int,IN `uniacid1` int)
    BEGIN
    select merchname,address,lng,lat,ser.*, ROUND(
    
            6378.138 * 2 * ASIN(
    
                SQRT(
    
                    POW(
    
                        SIN(
    
                            (
    
                                lat1 * PI() / 180 - lat * PI() / 180
    
                            ) / 2
    
                        ),
    
                        2
    
                    ) + COS(lat1 * PI() / 180) * COS(lat * PI() / 180) * POW(
    
                        SIN(
    
                            (
    
                                lon1 * PI() / 180 - lng * PI() / 180
    
                            ) / 2
    
                        ),
    
                        2
    
                    )
    
                )
    
            ) * 1000
    
        ) AS juli from merch_service  as ser
    inner join merch on ser.merchid=merch.id
    where merch.`status` in (1,3)
    and ser.type=type
    and ser.uniacid=uniacid1
    and merch.uniacid=uniacid1
    ORDER BY  juli asc
    LIMIT PageStart,PageStep;
    END

    复杂点可以用

    CREATE DEFINER=`ln` PROCEDURE `Proc_3`(IN `lon1` double,IN `lat1` double,IN `PageStart` int,IN `PageStep` int,IN `type` varchar(20),IN `uniacid` int,IN `iscommand` int,IN `distance` int)
    BEGIN
    IF iscommand = 0 THEN  
        set @isCommand= ' order by juli asc';  
    ELSE  
        set @isCommand= ' order by isrecommand desc ,juli asc';  
    END IF;
    IF distance = 0 THEN  
        set @distance= ' ';  
    ELSE  
        set @distance= CONCAT(' and `juli` <= ', distance);  
    END IF;
    set @type = CONCAT('%',type,'%');
    set @_where = " where `status` IN(1,3)";
    set @_where = CONCAT(@_where,' AND `diyformdata` like "',@type,'"');
    set @_where = CONCAT(@_where,' AND `uniacid` = ', uniacid);
    set @_where = CONCAT(@_where,@distance);
    set @sql = CONCAT(
    "SELECT * FROM
    (SELECT 
        *,
    
        ROUND(
    
            6378.138 * 2 * ASIN(
    
                SQRT(
    
                    POW(
    
                        SIN(
    
                            (
    
                                ",lat1," * PI() / 180 - lat * PI() / 180
    
                            ) / 2
    
                        ),
    
                        2
    
                    ) + COS(",lat1," * PI() / 180) * COS(lat * PI() / 180) * POW(
    
                        SIN(
    
                            (
    
                                ",lon1," * PI() / 180 - lng * PI() / 180
    
                            ) / 2
    
                        ),
    
                        2
    
                    )
    
                )
    
            ) * 1000
    
        ) AS juli
    
    FROM
    
        merch)a ");
    set @sql = CONCAT(@sql,@_where,@isCommand, ' limit ', PageStart, ',', PageStep);
    prepare stmt from @sql; -- 预编释一下。 “stmt”预编释变量的名称,  
    execute stmt; -- 执行SQL语句  
    deallocate prepare stmt; -- 释放资源  
    
    END
  • 相关阅读:
    OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/django'
    mac 安装pip
    同学公司倒闭了
    web开发中的字体选择(同事分享)
    svg 学习笔记
    用highchaarts做股票分时图
    highcharts,highStock 中文图表配置
    为什么使用 npm Scripts 构建项目
    JS 浮点型计算的精度问题 推荐的js 库 推荐的类库 Numeral.js 和 accounting.js
    HTML代码转换为JavaScript字符串
  • 原文地址:https://www.cnblogs.com/kkform/p/9236171.html
Copyright © 2020-2023  润新知