PHP mySQL查询后的订单结果

我有一个包含邮政编码及其地理坐标的数据库。数据看起来类似于以下PHP mySQL查询后的订单结果

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查询后的订单结果 的全部内容, 来源链接: utcz.com/qa/257765.html

回到顶部