我创造了一些类似的东西并且可以工作
INSERT INTO summary (user_id1, user_id2, distance, points)
SELECT a1.userid, a2.userid,
GetDistances(a1.lat,a1.lng,a2.lat,a2.lng) as distance,
GetDistance(a1.lat,a1.lng,a2.lat,a2.lng) as points FROM location a1 JOIN location a2 ON a1.userid <> a2.userid
ON DUPLICATE KEY UPDATE
distance=VALUES(distance),
points=points+values(points);
积分
DELIMITER $$
DROP FUNCTION IF EXISTS `GetDistance`$$
CREATE FUNCTION `GetDistance`(
lat1 numeric (9,6),
lon1 numeric (9,6),
lat2 numeric (9,6),
lon2 numeric (9,6)
) RETURNS decimal (10,5)
BEGIN
DECLARE x decimal (20,10);
DECLARE pi decimal (21,20);
DECLARE final decimal (10,5);
DECLARE newvalue decimal (10,5);
SET pi = 3.14159265358979323846;
SET x = sin( lat1 * pi/180 ) * sin( lat2 * pi/180 ) + cos(
lat1 *pi/180 ) * cos( lat2 * pi/180 ) * cos( abs( (lon2 * pi/180) -
(lon1 *pi/180) ) );
SET x = atan( ( sqrt( 1- power( x, 2 ) ) ) / x );
SET final = ( 1.852 * 60.0 * ((x/pi)*180) ) / 1.609344;
case WHEN final < 247 THEN
SET newvalue = 2;
WHEN final > 600 THEN
SET newvalue = 1;
else SET newvalue = 0;
END CASE;
RETURN newvalue;
END $$
DELIMITER ;
对于距离
DELIMITER $$
DROP FUNCTION IF EXISTS `GetDistances`$$
CREATE FUNCTION `GetDistances`(
lat1 numeric (9,6),
lon1 numeric (9,6),
lat2 numeric (9,6),
lon2 numeric (9,6)
) RETURNS decimal (10,5)
BEGIN
DECLARE x decimal (20,10);
DECLARE pi decimal (21,20);
SET pi = 3.14159265358979323846;
SET x = sin( lat1 * pi/180 ) * sin( lat2 * pi/180 ) + cos(
lat1 *pi/180 ) * cos( lat2 * pi/180 ) * cos( abs( (lon2 * pi/180) -
(lon1 *pi/180) ) );
SET x = atan( ( sqrt( 1- power( x, 2 ) ) ) / x );
RETURN ( 1.852 * 60.0 * ((x/pi)*180) ) / 1.609344;
END $$
DELIMITER ;