简体   繁体   English

如何从PHP中的mysql查询中获取最高和最低纬度和经度

[英]How to get Highest and Lowest Latitude and Longitude from mysql query in PHP

I have a database of addresses and I'm setting up a map search option on my website. 我有一个地址数据库,并且正在我的网站上设置一个地图搜索选项。 The database has data such as address, price, numberOfBedrooms, numberOfBathrooms, Latitude, Longitude, etc. A user has the option to search by City and Price. 该数据库具有诸如地址,价格,BedOfBedrooms,BathOfBathrooms,纬度,经度等数据。用户可以选择按城市和价格进行搜索。 Once a user clicks the search button I construct my sql query and return the results. 用户单击搜索按钮后,我将构建我的sql查询并返回结果。 How can I get the latitude and longitude range in 4 separate variables (ie. Highest Lat, Lowest Lat, Highest Long, and Lowest Long) in order to set the boundaries for google maps? 如何设置4个单独变量(即最高纬度,最低纬度,最高纬度和最低纬度)的纬度和经度范围,以便为Google地图设置边界?

mysql query example: mysql查询示例:

SELECT * FROM table WHERE City = "city" AND Price BETWEEN "100000" AND "200000"

Let's say it returned 50 results. 假设它返回了50个结果。 Would it be more efficient to do more sql queries to find the min and max lat/lng coordinates, or is there a way to loop through the results to find the min/max lat/lng using only one query that would be quicker? 进行更多的sql查询以查找最小和最大lat / lng坐标会更有效,还是有一种方法可以通过仅使用一个更快的查询来遍历结果以查找最小/最大lat / lng?

EDIT: I should point out that I also need it to return all the other columns as well as the highest and lowest lat/lng 编辑:我应该指出,我还需要它返回所有其他列以及最高和最低的lat / lng

You could do this in MySQL < 8 like this: 您可以在MySQL <8中这样做,如下所示:

SELECT id, price, lat, lng, maxlat, minlat, maxlng, minlng
FROM places
FULL  JOIN
    (SELECT MAX(lat) maxlat, MIN(lat) minlat, MAX(lng) maxlng, MIN(lng)          
            minlng FROM places) a
WHERE price BETWEEN 100 AND 250

This would set the bounds to the full dataset. 这会将边界设置为完整数据集。 If you wanted the bounds to be within the result set you would need to copy the WHERE conditions into the subquery. 如果您希望范围在结果集中,则需要将WHERE条件复制到子查询中。 This requires two scans of the table so you need to determine if this is efficient in your case. 这需要对表进行两次扫描,因此您需要确定这种情况是否有效。

With MySQL version 8 you can define the query once and scan the results as follows: 使用MySQL版本8,您可以一次定义查询并按以下方式扫描结果:

WITH myQuery AS
(SELECT id, price, lat, lng
FROM places
WHERE price BETWEEN 100 AND 250)

SELECT * FROM myQuery FULL JOIN (SELECT Max(lat), MIN(lat), MAX(lng), MIN(lng) FROM myQuery) a

This is probably the most efficient SQL-based solution. 这可能是最有效的基于SQL的解决方案。

Otherwise you can do: 否则,您可以执行以下操作:

$result = mysqli_query($connection,"SELECT id, price, lat, lng
FROM places
WHERE price BETWEEN 100 AND 250");

while ($result = mysqli_fetch_assoc($result)) {
     $maxLng = (!isset($maxLng) || $result['lng'] > $maxLng) ? $result['lng'] : $maxLng;
     //etc..
}

This would be the most efficient if you are already looping over your results as part of your existing code, which I suspect you are. 如果您已经将结果作为现有代码的一部分循环了,那将是最有效的,我怀疑您是这样。

Fiddle here: https://www.db-fiddle.com/f/67ff6UpKC2YT5yFudrZZH3/0 在这里提琴: https : //www.db-fiddle.com/f/67ff6UpKC2YT5yFudrZZH3/0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM