最新的省市區三級地區MySQL數據庫


想要直接拿MySQL地區sql文件的,直接跳到文章末尾獲取

前言

    之前也寫過類似的獲取方式,是從國家統計局獲取5級地區信息,方法也比較麻煩,在實際使用過程中,也很少遇到要精確到5級的情況,所以,這次就更新一下,如何獲取三級地區信息。

1、獲取數據
進入高德地圖的官網,下載到我們需要的數據:高德地圖官網

 

我當時的獲取位置是:開發支持==>web端==>地圖JS API,拉到最下面,就能看到相關下載鏈接,點擊后,就是上面圖片的位置。

下載后,將得到一個citycode.xlsx的Excel文檔,這個文檔不能直接使用,需要做一下轉換,將Excel變成csv文件,才能做進一步的操作。

2、數據處理
在Windows電腦上,將上面的citycode.xlsx文件打開,另存為csv文件,注意是逗號分隔的;

最后還需要調整一下編碼格式,可以用記事本等工具把導出的citycode.csv文件打開,重新保存一下,保存的時候,選擇UTF-8的中文編碼格式,得到最后的citycode2.csv文件;

做完上述的步驟之后,就可以導入到數據庫中了,關於如何導入,有兩種方式:

一、參照我之前寫的一篇博文:MySQL導入導出csv文件,用命令導入

二、用Navcat工具導入,步驟如下:

1、選擇一個數據庫,在table選項上右鍵==>Import Wizard

2、選擇CSV File(*.csv),點擊Next

3、Improt From選擇我們上面的citycode2.csv,編碼格式UTF-8(65001)

4、選擇行分隔符為CRLF(Windows系統用的是這個,鑒於我們導出的CSV文件也是office導出的,office又是運行在Windows上的,所以這里不會有變化),分隔符用默認的逗號,如果不放心,自己再打一個英文逗號

 

5、之后就可以一直下一步了,最后確認導入。

6、最后,我們得到一張citycode2的數據庫表,如下:

 

 

 

3、信息整理
我們拿到的這么一張數據庫表,並不能直接使用,像地區等級,上下級等等的信息都沒有,所以還需要加工處理一下才能使用

我這邊是寫了一個函數,將數據全部清理了一遍,導入到新的地區表中了,下面是新建的數據庫表,以及我編寫的函數:

CREATE TABLE `sys_position` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`area_name` varchar(255) DEFAULT NULL COMMENT '地區名稱',
`area_code` int(11) DEFAULT NULL COMMENT '地區編碼',
`city_code` varchar(11) DEFAULT NULL COMMENT '城市編碼',
`level` tinyint(1) DEFAULT NULL COMMENT '地區等級',
`area_index` varchar(255) DEFAULT NULL COMMENT '地區索引',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3537 DEFAULT CHARSET=utf8mb4;
drop function if exists create_city;
delimiter $$
create function create_city(provinceCode varchar(11))
returns varchar(20)
begin
declare intProvinceCode int(11);
declare intCityCode int(11);
declare loopTimes int(11) default 0;
declare areaCityCode varchar(11);
declare i int(11);
declare tempPage int(11) default 0;
declare countyIndex varchar(36);
set intProvinceCode = CONVERT(provinceCode,SIGNED);
-- 當前省下面有多少個市
select COUNT(1) into loopTimes from citycode2 where adcode between intProvinceCode and intProvinceCode+10000 and substring(adcode,3) != "0000" and substring(adcode,5) ="00";
SET i = 1;
-- 1.1先插入市的數據
insert into sys_position (area_name,area_code,city_code,`level`)
select area_name,adcode,citycode,2 from citycode2
where adcode between intProvinceCode and intProvinceCode+10000 and substring(adcode,3) != "0000" and substring(adcode,5) ="00";
-- 1.2補充市的上級地區索引
update sys_position set area_index=provinceCode where area_code between intProvinceCode and intProvinceCode+10000
and substring(area_code,3) != "0000" and substring(area_code,5) ="00";
-- 2循環更新縣的數據
WHILE i <= loopTimes DO
SET tempPage = i-1;
-- 2.1獲取市的編碼
select adcode into areaCityCode from citycode2 where adcode between intProvinceCode and intProvinceCode+10000 and substring(adcode,3) != "0000" and substring(adcode,5) ="00" limit tempPage,1;
SET intCityCode = CONVERT(CONCAT(substring(areaCityCode,3,2),"00"),SIGNED);
SET intCityCode = intProvinceCode+intCityCode;
-- 2.2插入縣的數據
insert into sys_position (area_name,area_code,city_code,`level`)
select area_name,adcode,citycode,3 from citycode2
where adcode between intCityCode and intCityCode+100 and substring(adcode,3) != "0000" and substring(adcode,5) !="00";
-- 2.3補充縣的上級地區索引 這里需要循環取出
SET countyIndex = CONCAT(provinceCode,",",areaCityCode);
update sys_position set area_index=countyIndex
where area_code between intCityCode and intCityCode+100 and substring(area_code,3) != "0000" and substring(area_code,5) !="00";
SET i = i + 1;
END WHILE;
-- 補處理沒有市信息,直接是區級別的
-- 3.1 如果省下面一個市(5-6位是0)也沒有 將區級別的直接歸為市
IF loopTimes = 0 THEN
insert into sys_position (area_name,area_code,city_code,`level`,area_index)
select area_name,adcode,citycode,2,provinceCode from citycode2
where adcode between intProvinceCode and intProvinceCode+10000 and substring(adcode,3) != "0000";
-- 3.2 如果是省,取上述循環結束后的地區編碼,表格是按照數字增長的,所以將沒有上級市的“市”也划為市
ELSE
insert into sys_position (area_name,area_code,city_code,`level`,area_index)
select area_name,adcode,citycode,2,provinceCode from citycode2
where adcode between intCityCode+100 and intProvinceCode+10000 and substring(adcode,3) != "0000";
END IF;
return 'hello';
end$$
delimiter ;
最后執行了一條sql語句:

select create_city(adcode) from citycode2 where substring(adcode,3)="0000" and adcode!="100000" and adcode!="900000"
函數的內容有點復雜,這里就不詳細去講解了,有興趣的人可以分析一下

另外需要補充的一點是,從原表到新表,少了1條數據,我檢查了很久也不知道是哪里出了問題,由於影響很小,我這里也就沒管了,有小伙伴看完之后,幫忙修復上述函數的漏洞或者補充出來這1條數據,也是極好的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM