mysql的距离矩阵,基于haversine距离计算的先前解决方案

mysql的距离矩阵,基于haversine距离计算的先前解决方案,mysql,matrix,distance,self-join,Mysql,Matrix,Distance,Self Join,这个问题的自我回答是为了帮助需要在MySQL中创建距离矩阵的人,MySQL支持以下查询:“从距离中选择id2,其中id1=10,type1=0,type2=1和Distance为了坚持问答格式,您应该将此作为一个问题提出,然后用您提出的解决方案回答您自己的问题。不幸的是,对于低级别用户,提问和回答之间有8个小时的延迟。谢谢大家的关注,已经8个多小时了。如果您编辑此问题并选择自己的答案作为正确答案,您可以获得额外的徽章(在本网站上共享知识没有错) select o1.object_id AS id

这个问题的自我回答是为了帮助需要在MySQL中创建距离矩阵的人,MySQL支持以下查询:“从距离中选择id2,其中id1=10,type1=0,type2=1和Distance为了坚持问答格式,您应该将此作为一个问题提出,然后用您提出的解决方案回答您自己的问题。不幸的是,对于低级别用户,提问和回答之间有8个小时的延迟。谢谢大家的关注,已经8个多小时了。如果您编辑此问题并选择自己的答案作为正确答案,您可以获得额外的徽章(在本网站上共享知识没有错)
select o1.object_id AS id1,o1.object_type AS type1,o2.object_id AS id2,o2.object_type AS type2,geodistance_km_by_obj(o1.object_id,o1.object_type,o2.object_id,o2.object_type) AS distance from (Coordinates o1 join Coordinates o2) where ((o1.object_id,o1.object_type) <> (o2.object_id,o2.object_type))
CREATE FUNCTION geodistance_km_by_obj(object_id1 INT, object_type1 TINYINT, object_id2 INT, object_type2 TINYINT)
RETURNS float
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
SQL SECURITY INVOKER
COMMENT 'returns distance in km'
BEGIN
declare sl1 float;
declare cc1 float;
declare cs1 float;
declare sl2 float;
declare cc2 float;
declare cs2 float;

select sin_lat,cos_cos,cos_sin into sl1, cc1, cs1 from Coordinates where object_id=object_id1 and object_type=object_type1;
select sin_lat,cos_cos,cos_sin into sl2, cc2, cs2 from Coordinates where object_id=object_id2 and object_type=object_type2;
return cast(round(acos(sl1*sl2 + cc1*cc2 + cs1*cs2)*6371,0) as decimal);
END