我在使用此查询逻辑时遇到了困难。我传入一个带坐标的数组,这个坐标创建了一个可见区域的框。我正试图在该框内返回任何可能的位置。坐标数组如下所示:
$array['farLeftLat']=>value;
$array['farLeftLon']=>value;
$array['farRightLat']=>value;
$array['farRightLon']=>value;
$array['nearLeftLat']=>value;
$array['nearLeftLon']=>value;
$array['nearRightLat']=>value;
$array['nearRightLon']=>value;
在我的数据库中,该行有两列作为纬度和经度:
id | latitude | longitude | .....
1 | value | value | .....
2 | value | value | .....
我需要将纬度和经度的数据库行实例分组并检查farLeft,farRight,nearLeft,nearRight ......
"SELECT * FROM locations WHERE (//row instance of lat<farLeftLat and row instance of lon>farLeftLon)
AND (//row instance of lat<farRightLat and row instance of lon<farRightLon)..."
解答:
更仔细地查看坐标后,得到了更好的理解。我只需要四个坐标:顶部和底部线(纬度)和左右线(经度)。所以我的查询看起来像这样:
"SELECT * FROM locations WHERE (latitude BETWEEN $array['nearLeftLat']
AND $array['farLeftLat']) AND (longitude BETWEEN $array['farLeftLon'] AND $array['farRightLon'])"
//wrong syntax with 's but I declared variables in my code.
这里的诀窍是,较小的值必须先来......也会陷入困境。