2016-01-13 32 views
0

其實很簡單,我只是不知道該怎麼做。根據距離返回匹配的sql行

我有一個表:

table locations 
location_id  coordinates name 
------------------------------------- 

當我接收從I它存儲變量的內部在POINT類型的MySQL的(長,緯度)的設備的緯度和經度。

我試圖獲取位置表中距離小於或等於八米的所有行。

我的查詢如下:

SELECT * FROM locations where st_distance_sphere(@userCoords, @pt1) <= 8; 

我知道這個查詢是錯誤的,因爲@pt1不與列座標有關。如何隔離座標列中的特定值並返回匹配的行的列表?

st_distance_sphere()函數只是計算兩個座標之間的距離並返回一個數字。

預先感謝您!

回答

2

所有你需要的是把柱,而不是@pt1(假設列類型爲點):

SELECT * 
FROM locations 
where st_distance_sphere(@userCoords, coordinates) <= 8; 

這會給你,他們的座標是8或更少@userCoords所有行

+0

救主。感謝您幫助newb:D – Mihado