根据距离排序
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