gpt4 book ai didi

PHP mySQL 查询后对结果进行排序

转载 作者:行者123 更新时间:2023-11-29 07:29:26 24 4
gpt4 key购买 nike

我有一个数据库,其中包含邮政编码及其地理坐标。数据类似于以下内容

 id | Postcode   | Longitude  |  Latitude      |  
-------------------------------------------------
1 | W12 7GF | 51.51527 | -0.08816 |
-------------------------------------------------
2 | SW16 6GF | 51.51528 | -0.15960 |
-------------------------------------------------
3 | W1 4FT | 51.51528 | -0.11590 |
-------------------------------------------------

我首先要做的是输入邮政编码(我现在已经对其进行了硬编码)。

$sql = "SELECT * FROM postcodes WHERE `postcode` = 'W14 6TY'";

执行此操作后,我会获得该邮政编码的经度和纬度。我还设置了更多变量。

$lat1 = $row['Latitude'];
$lon1 = $row['Longitude'];
$d = 5;
$r = 3959;

现在我要做的是获取上述邮政编码 5 英里半径内的所有其他邮政编码。为此,我这样做

$latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
$latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
$lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));

$query = "SELECT * FROM postcodes WHERE (Latitude <= $latN AND Latitude >= $latS AND Longitude <= $lonE AND Longitude >= $lonW) AND (Latitude != $lat1 AND Longitude != $lon1) ORDER BY Latitude, Longitude ASC LIMIT 30";
$result2 = $conn->query($query);

如您所见,我限制了结果,因为我不希望返回数百个结果。最后输出数据

echo "<div class='container'>";
echo "<div class='row'>";
echo "<div class='col-md-12 col-sm-12 col-xs-12'>";
echo "<table class=\"table table-striped\">";
echo "<tr><th>Postcode</th><th>Latitude</th><th>Longitude</th><th>Miles, Point A To B</th></tr>\n";
while ($row = $result2->fetch_assoc()) {
echo "<tr><td>$row[Postcode]</td><td>$row[Latitude]</td><td>$row[Longitude]</td>";
echo "<td>".acos(sin(deg2rad($lat1)) * sin(deg2rad($row['Latitude'])) + cos(deg2rad($lat1)) * cos(deg2rad($row['Latitude'])) * cos(deg2rad($row['Longitude']) - deg2rad($lon1))) * $r."</td>";
echo "</tr>\n";
}
echo "</table>\n<br />\n";
echo "</div>";
echo "</div>";
echo "</div>";

正如您在输出中看到的,我还计算了从 A 点到 B 点的英里数。我无法在数据库查询级别执行此操作,因为我需要对生成的地理坐标进行所有数学运算。

目前,数据按纬度和经度排序。因为这些数字没有多大意义,所以输出看起来有点滑稽。

我的问题是,是否可以根据点之间最小到最大的英里数对输出进行排序?我想我需要删除限制(这样它可以在所有输出上工作),但不确定我是否可以做到这一点,因为我直到查询之后才计算它。

任何建议表示赞赏。

谢谢

最佳答案

例如

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Postcode VARCHAR(12) NOT NULL
,Longitude DECIMAL(8,5)
,Latitude DECIMAL(8,5) NOT NULL
);

INSERT INTO my_table VALUES
(1 ,'W12 7GF',51.51527,-0.08816),
(2 ,'SW16 6GF',51.51528,-0.15960),
(3 ,'W1 4FT',51.51528,-0.11590),
(4 ,'W14 8UX',51.49645,-0.20975);

SELECT y.*
, ROUND(geo_distance_km(x.latitude,x.longitude,y.latitude,y.longitude),2) dist
FROM my_table x
JOIN my_table y
ON y.id <> x.id
WHERE x.postcode = 'W14 8UX';
+----+----------+-----------+----------+-------+
| id | Postcode | Longitude | Latitude | dist |
+----+----------+-----------+----------+-------+
| 1 | W12 7GF | 51.51527 | -0.08816 | 13.69 |
| 2 | SW16 6GF | 51.51528 | -0.15960 | 5.96 |
| 3 | W1 4FT | 51.51528 | -0.11590 | 10.65 |
+----+----------+-----------+----------+-------+

显然,我在这个答案中遗漏了一个关键的部分。我想知道你是否能自己弄清楚这一点。

关于PHP mySQL 查询后对结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33224330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com