PHP&MySQL 商店距离计算器 - 邮政编码
是的,我一直在尝试找出如何将给定的邮政编码与商店地址的数据库进行比较,并根据最接近给定的邮政编码(或我猜的邮政编码)的方式对它们进行排序。
这主要是出于兴趣,而不是我向您寻求建议然后将其出售给客户:-O
首先,经过研究,我发现您必须与纬度/经度进行距离计算,所以我找到了一个可以转换邮政编码/的API邮政编码转换为经纬度,现在我的数据库具有诸如id,store_name,lat,long,postcode
之类的结构,我可以将给定的邮政编码转换为经纬度。
但是,如何在 SQL 中查询最接近给定经纬度的值呢?
Right I have been trying to work out how to compare a given postcode to a database of say store addresses and have them ordered in terms of which one is closest to the given postcode (or ZIP code I guess).
This is mainly out of interest, rather than me asking you for advice and then selling it to a client :-O
First of all after research I discovered that you have to do distance with Lat/Long so I found an API that converts postcodes/zip codes to lat long and now my DB has a structure such as id, store_name, lat, long, postcode
and I can convert a given postcode to a lat long.
But how in SQL do I make a query for the ones closest to a given lat long?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的事情:
更新:
有一些关于效率的讨论。这是针对此查询的一个小基准。我有一个包含美国每个邮政编码的数据库。由于邮政编码的工作方式,其中一些是重复的(超出了本主题的范围)。所以我有不到 80k 条记录。我在 90210 上跑了 20 英里半径的距离:
我得到了总共 366 条记录,并且查询花费了0.1770 秒。您需要提高多少效率?
Try something like this:
UPDATE:
There is some discussion about efficiency. Here is a little benchmark for you with this query. I have a database that contains EVERY zipcode in the US. Some of them are duplicate because of the way zipcodes work (outside the scope of this topic). So I have just under 80k records. I ran a 20 mile radius distance on 90210:
I got back 366 total records and Query took 0.1770 sec. How much more efficient do you need?
查看这个伟大的开源项目
免责声明:不是我的项目,我也不是贡献者。纯粹是推荐。
check out this great open source project
Disclaimer: Not my project, and nor am I contributor. Purely a recommendation.
有关示例,请参阅对上一个问题的回答在查询 MySQL 之前计算边界框。这允许 MySQL 查询中的复杂公式针对数据库条目的子集运行,而不是针对表中的每个条目。
See this answer to a previous question for an example of calculating a bounding box before querying MySQL. This allows the complex formula in the MySQL query to run against a subset of the database entries, rather than against every entry in the table.