zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    3-附1 ->和*的区别
    第2章 变量和基本类型
    第2章 变量和基本类型 附3---底层const和顶层const
    第2章 变量和基本类型 附2 --声明和定义的区别
    第2章 变量和基本类型 附1---变量和对象的区别
    第一章 开始
    1. 数据采集基础问题
    跟波利亚学解题---1
    [PTA]L2-001 紧急救援 (25 分)
    [图论]最短路计数(spfa)
  • 原文地址:https://www.cnblogs.com/kkform/p/9236171.html
Copyright © 2011-2022 走看看