一般的邮政地址都要包含国家、省份、市县乡之类的数据,大家一般都是怎么存储的?
回复内容:
一般的邮政地址都要包含国家、省份、市县乡之类的数据,大家一般都是怎么存储的?
基本上摘自我在sf的博客文章:http://blog.segmentfault.com/shamiao/1190000000329012。
我喜欢用左右值法来组织省-市-区的表格。
数据结构
------------------------------------- name lbb ubb depth 注:ID省略 ------------------------------------- 吉林省 1 14 1 长春市 2 7 2 朝阳区 3 4 3 南关区 5 6 3 辽源市 8 13 2 龙山区 9 10 3 东丰县 11 12 3 辽宁省 15 18 1 沈阳市 16 17 2 -------------------------------------
数据表就像这样。这个组织方法的特点是:
- 每一个区划覆盖一个整数值区间。
- 各个区间只嵌套不交叉。
- 甚至各区间的上下限不允许重叠,左右值是无重复的。
这是一个相当重要的优化。下边的查询,没有这个条件基本都不能成立或不太方便。 - 最小的区间长度至少为1。
- 总体是一个无限嵌套的结构。
以上边这个表格为例,他表示了这样一个分层结构:
1------------------吉林-------------------14 15---辽宁---18 2------长春------7 8--------辽源--------13 16-沈阳-17 3-朝阳-4 5-南关-6 9-龙山-10 11-东丰-12
这样,省-市-区就只需要存储一个数字,即该区划的左值,就可以了。
但必须注意:虽然存左值完全够实用了,但为了数据安全,必须另开设一个字段存储对应区划的ID,作为“真正唯一的关联数据(虽然不怎么用得到)”。而左值只能当作“非常有用的冗余数据”,做好一更新随时会被重写的准备。原因末尾“优势和弱点”一节会解释。
查询方法
根据号码查询单个区划的名称,查左值相等的就行了。
如果要列出所有的上级区划,尤其方便。由于区间包含是一个简单的数学关系,所以再也不必要像典型的存储父记录号码那样,费时费力去做回溯操作。
只要把包含区间的值的所有区间取出,就是这个区划的所有上级。并且,由于大区间的左值一定比小区间的小,所以只需要按照左值升序,就可以把区间从大到小排序。
例如查询朝阳区(左值=3)
的所有上级:
SELECT * FROM regiontable WHERE lbb<=3 AND ubb>3 ORDER BY lbb ASC
得到结果:
------------------------------------- name lbb ubb depth ------------------------------------- 吉林省 1 14 1 长春市 2 7 2 朝阳区 3 4 3 -------------------------------------
即吉林省-长春市-朝阳区。
遍历方法
数据表中存储了depth
冗余字段代表层级深度。这个字段会在查询中起大作用。
例如列出下级所有区域(已知当前级别的左右界和深度):
SELECT * FROM regiontable WHERE lbb>左界 AND ubb<右界 AND depth=(深度+1) ORDER BY lbb ASC
如果列出第一级那就只查depth=1就行了。
这个查询不仅可以用于行政区划表格,也可以用于用户数据表格。例如列出属于某区域的所有用户(已知此级别的左右界):
SELECT * FROM userinfo WHERE region>=左界 AND region<右界
而无论对于多大的区域,这个查询效率的都是均等的高,彻底杜绝区域大了查不动。
优势和弱点
左右值法快就快在查询和排序都有数学的自然性。一步到位,无需回溯,效率是显而易见的。
而弱点同样显而易见:修改极其麻烦,并且往往是牵一发而动全身。一处写入,估计大半张表格都要跟着修改。算法就会很麻烦。
而这要注意对于任何一个记录,其左右值都是不稳定的,只能用于查询,绝对不能用来与数据表建立持久稳定的关系!这也就是前边说过一定要存区划ID的意义。
这个需求中,我们恰好使用了优势,而规避了弱点。因为行政区划数据天天查,但很少改。
数据哪里来?
要求不高的,维基百科去抄,或者到别的程序里去扒。
要求高的,去买民政部出的《中华人民共和国行政区划简册2013》,权威性没商量。
注意港澳台的行政区划问题。我建议以下的方案:
- 香港、澳门特别行政区,仅列在中华人民共和国下,不列入单独的国家/地区。
- 中华人民共和国下存在台湾省,但不再向下延伸;
- 第一级国家/地区(如果有的话)单列台湾,并按维基百科资料,或参考台湾执政部门资料,填入对应的下级行政区域。最好使用正体(繁体)中文。
- 另外,小心使用洋人做的国家列表。要是一不小心新疆和西藏单列了国家和地区,就自己考虑后果吧。
具体地址怎么存?
像淘宝那样,把省市区放在一行内,摆在地址文本框的旁边,并明示用户:地址中不必再输入省市区。
如何应对不存在的省市区名称?
我推荐的方法是:提示用户能选到多细选多细,如果再细没有了,就放在具体地址前边,一起输入到地址文本框中。
我是绝对反对整几个小文本框,给用户在找不到自己的省市区的时候,去自定义输入的。
理由也很简单:就算我们的行政区划数据库再老,再不准确,那也能保证99%的人都能找到自己的地址。
所以这个思路看似很自然(没有就自定义嘛!),但其实是在为了1%的需求投入100%的开发精力,到头来是极其愚蠢和低效的。
HTML 表格中有几个数据段, 我的数据库就相应的有几个column.
国家,省份,城市名称,我是这么干的:
plus_id, country, province, city 1 , 中国 , 北京 , null 2 , 中国 , 河北 , 廊坊
然后info表里关联ID就行。

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.