数据库中的邮政编码到城市/州,反之亦然?

发布于 2024-07-13 04:54:51 字数 261 浏览 10 评论 0原文

我是 SQL 和关系数据库的新手,我遇到了一个我认为是常见的问题。

我正在制作一个网站,当每个用户提交帖子时,他们必须提供邮政编码或城市/州的位置。

处理这个问题的最佳实践是什么? 我是否只需创建一个邮政编码、城市和州表并对其进行查询,或者是否有现成的解决方案来处理此问题?

如果有什么不同的话,我正在使用 SQL Server 2005。

我需要能够检索给定城市/州的邮政编码,或者我需要能够根据给定邮政编码说出城市/州。

I'm new to SQL and relational databases and I have what I would imagine is a common problem.

I'm making a website and when each user submits a post they have to provide a location in either a zip code or a City/State.

What is the best practice for handling this? Do I simply create a Zip Code and City and State table and query against them or are there ready made solutions for handling this?

I'm using SQL Server 2005 if it makes a difference.

I need to be able to retrieve a zip code given a city/state or I need to be able to spit out the city state given a zip code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

药祭#氼 2024-07-20 04:54:52

你有几个选择。 您可以从某人那里购买批量邮政编码库,其中会列出邮政编码、城市、县等,或者您可以付费让某人访问网络服务,该服务将在更细粒度的级别。

最好的选择是使用邮政编码库选项,因为它的成本低于 Web 服务,并且提供更好的性能。 如何查询或预处理该库取决于您。 您提到了 SQL Server,因此您可能需要“州”、“邮政编码”和“城市”表,并包括它们之间的相关关系。 您还需要针对跨越多个邮政编码的城市或具有多个城市的邮政编码进行规定 - 但这些问题都不是不可克服的。

至于处理用户输入的变化无常,您可以考虑寻求 地址验证网络服务,尽管大多数都需要完整的送货地址才能进行验证。

编辑:看起来有一个 SourceForge 项目提供免费的邮政编码数据,包括纬度/经度数据等不确定它有多正确或最新。

编辑 2:粗略地浏览了 SourceForge 项目的网站后,它看起来像是一个死项目。 如果您使用此数据,您需要为数据库中不存在的邮政编码/城市提供一些津贴。 购买的批量库通常带有某种更新保证或更新定价计划等,并且可能更可靠。

You have a couple options. You can buy a bulk zip-code library from somebody which will list zip codes, cities, counties, etc. by state, or you can pay someone to access a web service which will perform the same function on a more granular level.

Your best bet would be to go with the zip-code library option, as it'll cost you less than the web service and will provide better performance. How you query or pre-process this library is up to you. You mention SQL Server, so you'd probably want State, Zipcode, and City tables, and include the relevant relationships between them. You'll also need to have provisions for cities that span multiple zipcodes, or for zipcodes that have multiple cities - but none of these issues are insurmountable.

As far as dealing with the vagarities of user input, you may consider enlisting the help of an address validation web service, although most of them require a full shipping address in order to validate.

Edit: looks like there's a SourceForge project offering free zip-code data, including lat/lon data, etc. Not sure how correct or current it is.

Edit 2: After some cursory looking on that SourceForge project's site it looks like this is a dead project. If you use this data, you'll need to provide some allowance for zipcodes / cities that don't exist in your database. Purchased bulk libraries usually come with some sort of guarantee of updates, or a pricing plan for updates, etc., and are probably more reliable.

兲鉂ぱ嘚淚 2024-07-20 04:54:52

有一个与 CityState 表相关的 ZipCode 表。 某些邮政编码有多个与之关联的城市,因此您可能需要让界面让用户从他们想要的城市中选择或让他们覆盖默认城市。

我使用 ZipInfo.com 的付费服务,因为我需要其他信息,例如 lat/长、邮政编码类型和县。 随着新邮政编码的添加或与其他邮政编码的合并,邮政编码每年也会更改几次,因此您需要每年更新几次数据以保持一致。

Have a ZipCode table that is related to a CityState table. Some zip codes have multiple cities associated with them, so you may need to have the interface let the user select from the city they want or let them override the default.

I use the paid service from ZipInfo.com since I needed additional information such as lat/long, zip type and county. Zip codes also change several times as year as new zip codes are added or merged with others, so you will need to update your data a few times a year to stay consistent.

献世佛 2024-07-20 04:54:52

根据您要构建的内容的详细信息,Yelp 有一个免费的 neighborhoods API 这也许能为您提供您所需要的东西。 请务必检查他们的使用条款和其他内容,以确保您遵守规定。

我知道这不是一个以数据库为中心的答案,但是您所做的事情可能无法在数据库本身中得到最好的处理。

Depending on the details of what you're building, Yelp has a free neighborhoods API that may be able to get you what you need. Be sure to check their Terms of Use and stuff to make sure you stay in compliance.

I know this isn't a db centric answer, but what you're doing may not be best handled in the database itself.

同展鸳鸯锦 2024-07-20 04:54:52

如果查找的原因是为了用户的方便,这里有一种替代方法,不需要许可任何第三方数据库:

只要邮政编码匹配,只需从现有的名称/地址表中查找城市/州。 如果有人之前输入了该邮政编码,那么您可以从该条目中找到城市和州。 如果不存在先前的条目,则最坏的情况是用户必须输入城市和州。

该解决方案假设您的需求是为了方便用户。 如果您更关心城市、州、邮政编码的准确验证,那么您最好授权经过验证的数据库。

If the reason for the lookup is the user's convenience, here's an alternate approach that doesn't require licensing any third-party databases:

Just lookup the city/state from your existing name/address table, provided the zip code matches. If somebody has previously made an entry for that zip code, then you'll find the city and state from that entry. IF no previous entry exists, then worst case the user has to entry in the city and state.

This solution assumes your need is for convenience for the user. If you are more concerned about accurate validation of city, state, zip codes then you're better off licensing a verified database.

你如我软肋 2024-07-20 04:54:52

有许多免费的地理编码网络服务,您可以在其中获取来自城邦的邮政编码,反之亦然。 查看 GeoNames Web 服务。 您可以执行诸如检查数据库之类的操作,然后如果您要查找的内容不存在,请从网络服务中获取并添加它。

There are many free geo-coding webservices where you can get a zip from a city-state, and vice-versa. Take a look at the GeoNames webservice. You could do something like check your db, and then if what you are looking for is not there, grab it from the webservice and add it.

薄情伤 2024-07-20 04:54:52

我的理解是 USPS Web API 可以免费使用,但需要权限,具体取决于取决于许多因素,包括将使用它的程序的性质以及您需要数据的原因。

如果您有资格使用它,这可能是您所需信息的最准确来源。

My understanding is that the USPS web API is free to use, but requires permission that depends on a number of factors, including the nature of the program that will be using it and the reason you need the data.

If you qualify to use it, this would presumably be the most accurate source for the information you need.

唠甜嗑 2024-07-20 04:54:52

不确定我理解这个问题。 您是否需要允许其中一个,然后返回两者?

即使有可以购买的邮政编码/城市数据库,您也必须小心,因为某些城市跨越多个邮政编码,因此您不能总是朝该方向“计算”。 相反方向的类似问题。

Not sure I understand the question. Do you need to allow either and, later, return both?

You'll have to be careful, even with a zip/city database that can be purchased, since some cities span multiple zips, so you can't always "calculate" in that direction. Similar issue in the opposite direction.

琉璃梦幻 2024-07-20 04:54:52

您可以购买并导入一些库,但价格并不昂贵。 您对它们的问题是您将需要承担持续的维护成本(不是很多)。 邮政编码一直在变化,这意味着大约 6 个月后,您的数据就会稍微过时。 您可能想看看与谷歌地图等服务的接口。 这里我们使用混合方法。 我们每 6 个月花这笔钱更新数据,如果邮政编码不在表中,我们会根据谷歌地图进行验证,以确保输入的信息仍然有效,只是尚未输入进入我们的系统。 如果有效,我们会更新系统,如果无效,我们会让用户知道发生了错误。

另一种选择是 ping USPS 网站。 我相信他们有一个通过邮政编码查找城市/州的页面。

There are libraries that you can buy and import that aren't that expensive. Your problem with them is going to be that you will have an ongoing maintenance cost with it (not a lot). Zip codes change all the time, meaning that in 6 mos or so, your data will be slightly out of date. You might want to look at interfacing with a service like google maps. Here we use a hybrid approach. We spend the amount of the money for updates to the data every 6 months, and if the zip code isn't in the table we verify it against google maps to make sure that information entered is still valid and it just hasn't been entered into our system. If it is valid, we update the system, if not we let the user know that a mistake was made.

Another option would be to ping the USPS website. I believe they have a city/state look up page by zipcode.

是伱的 2024-07-20 04:54:52

对于美国州表,您可以使用以下查询,假设您在 States 表中有 StateIdStateName 并且有 StateId< /code> 自动递增。

此查询包含美国 50 个州...

INSERT INTO States
        ( StateName )
        VALUES
( 'Alabama'),
( 'Alaska'),
( 'Arizona'),
( 'Arkansas'),
( 'California'),
( 'Colorado'),
( 'Connecticut'),
( 'Delaware'),
( 'District of Columbia'),
( 'Florida'),
( 'Georgia'),
( 'Hawaii'),
( 'Idaho'),
( 'Illinois'),
( 'Indiana'),
( 'Iowa'),
( 'Kansas'),
( 'Kentucky'),
( 'Louisiana'),
( 'Maine'),
( 'Maryland'),
( 'Massachusetts'),
( 'Michigan'),
( 'Minnesota'),
( 'Mississippi'),
( 'Missouri'),
( 'Montana'),
( 'Nebraska'),
( 'Nevada'),
( 'New Hampshire'),
( 'New Jersey'),
( 'New Mexico'),
( 'New York'),
( 'North Carolina'),
( 'North Dakota'),
( 'Ohio'),
( 'Oklahoma'),
( 'Oregon'),
( 'Pennsylvania'),
( 'Puerto Rico'),
( 'Rhode Island'),
( 'South Carolina'),
( 'South Dakota'),
( 'Tennessee'),
( 'Texas'),
( 'Utah'),
( 'Vermont'),
( 'Virginia'),
( 'Washington'),
( 'West Virginia'),
( 'Wisconsin'),
( 'Wyoming');

For US States Tables, you can use following query, assuming you have StateId and StateName in the States table and you have StateId auto increment.

This query has 50 US States...

INSERT INTO States
        ( StateName )
        VALUES
( 'Alabama'),
( 'Alaska'),
( 'Arizona'),
( 'Arkansas'),
( 'California'),
( 'Colorado'),
( 'Connecticut'),
( 'Delaware'),
( 'District of Columbia'),
( 'Florida'),
( 'Georgia'),
( 'Hawaii'),
( 'Idaho'),
( 'Illinois'),
( 'Indiana'),
( 'Iowa'),
( 'Kansas'),
( 'Kentucky'),
( 'Louisiana'),
( 'Maine'),
( 'Maryland'),
( 'Massachusetts'),
( 'Michigan'),
( 'Minnesota'),
( 'Mississippi'),
( 'Missouri'),
( 'Montana'),
( 'Nebraska'),
( 'Nevada'),
( 'New Hampshire'),
( 'New Jersey'),
( 'New Mexico'),
( 'New York'),
( 'North Carolina'),
( 'North Dakota'),
( 'Ohio'),
( 'Oklahoma'),
( 'Oregon'),
( 'Pennsylvania'),
( 'Puerto Rico'),
( 'Rhode Island'),
( 'South Carolina'),
( 'South Dakota'),
( 'Tennessee'),
( 'Texas'),
( 'Utah'),
( 'Vermont'),
( 'Virginia'),
( 'Washington'),
( 'West Virginia'),
( 'Wisconsin'),
( 'Wyoming');
谎言 2024-07-20 04:54:52

我得到了那个节拍。 这是一个免费的(压缩,csv):

Hosted At maphacks.com

标头为
zip,city,state,latitude,longitude,timezone,dst

现在,您的潜在损失是您没有支付更新费用(这最终可能会咬您,但他们会尽力保持更新)

I got that beat. Here's a free one (zipped, csv):

Hosted At maphacks.com

Headers are
zip,city,state,latitude,longitude,timezone,dst

Now, your potential loss is that you aren't paying for updates (which can bite you in the end, but they try to keep it somewhat updated)

格子衫的從容 2024-07-20 04:54:52

我想添加这个网站。

http://www.unitedstateszipcodes.org/zip-code-database/

您可以如果您用于个人/教育,请免费下载数据(截至 2014 年 3 月 20 日)。

I would like to add this website.

http://www.unitedstateszipcodes.org/zip-code-database/

You can download the data for free if you use for Personal/Educational (as of 2014-03-20).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文