Calculating distance between 2 points with sql

对着背影说爱祢 提交于 2020-01-23 12:03:01

问题


I have to calculate the distance between 2 points, X and Y being the coordinates of the first point while axisX and axisY are the coordinates of the second point. My only lead is this SQL statement, however this SQL does not return the result I'm looking for. So can anyone help me identify any mistake I may have made with this statement?

string x = "SELECT TownName,Address,SUBSTRING(X, 1, 2) AS Xaxis, SUBSTRING(Y, 1, 2) AS Yaxis,Type,Availability,Price,Size,Bedrooms,Bathrooms,Contact,Screenshot1,Screenshot2,Screenshot3,Screenshot4 FROM House"+
                                    "WHERE (SQRT(POWER(X - "+axisX+" , 2) + POWER(Y - "+axisY+", 2)) <= 2000)";

回答1:


SELECT <replace with your select>,

3963.0 * ACOS (
    sin(SUBSTRING(X, 1, 2)/57.2958) * sin(" + axisX + "/57.2958) + cos(SUBSTRING(X, 1, 2)/57.2958) * cos(" + axisX + "/57.2958) * cos(" + axisY + "/57.2958 - SUBSTRING(Y, 1, 2)/57.2958))
as distance
 FROM House
ORDER BY distance

I'm assuming axisX and axisY are your co-ordinates. This uses the distance calculation technique which may provide you a more accurate reading.

http://www.meridianworlddata.com/Distance-Calculation.asp

Hope this helps.



来源:https://stackoverflow.com/questions/6105151/calculating-distance-between-2-points-with-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!