--查找数据库中5公里内的记录
仓经:120.675104
仓伟:31.320949
道路系数:1.5
SELECT * FROM
(SELECT *,((6371.004*ACOS(SIN(31.320949/180*PI())*SIN(地纬/180*PI())+COS(31.320949/180*PI())*COS(地纬/180*PI())*COS((120.675104-地经)/180*PI())))*1.5) AS dis FROM 测试表) AS AA WHERE 到仓距离<=5
在这里插入图片描述
函数写法
ALTER FUNCTION [GetDistance]
(
@GPSLng DECIMAL(12,6),
@GPSLat DECIMAL(12,6),
@Lng DECIMAL(12,6),
@Lat DECIMAL(12,6)
)
RETURNS DECIMAL(12,4)
AS
BEGIN
DECLARE @result DECIMAL(12,4)
SELECT @result=6371.004*ACOS(SIN(@GPSLat/180*PI())*SIN(@Lat/180*PI())+COS(@GPSLat/180*PI())*COS(@Lat/180*PI())*COS((@GPSLng-@Lng)/180*PI()))
RETURN @result
END
//查找数据库中5公里内的记录
SELECT *,GetDistance(当期点的经度,当期点的纬度,经度字段,纬度字段) AS dis FROM TABLENAME WHERE dis<5