postgres find zip codes near specific zip code

独自空忆成欢 提交于 2019-12-05 17:42:44

Something like this might be what you want:

SELECT …
FROM atable
WHERE zip = @zip

UNION ALL

SELECT …
FROM atable
WHERE NOT EXISTS (
  SELECT *
  FROM atable
  WHERE zip = @zip
)
  AND zip LIKE CONCAT(LEFT(@zip, 3), '%')

This may not be the most efficient solution, but at least it is a single query so might do well as a starting point.

ORDER BY the delta from the desired zip?

So, this is not specifically for PostgreSQL -- it was actually written for MySQL originally -- but it should work. I came across this article a while back that showed how to create a database containing zipcodes and latitude/longitude for each zipcode, then using trigonometry to calculate the distance between zip codes. Take a look at the link. I'm sure it will help you...

http://www.chrissibert.com/blog/2009/06/16/mysql-zip-codes-latitude-longitude-distance/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!