mysql 2018最新行政代碼導入方法(包括遞級關係)

簡單方便方法如下:

1.到民政部下載2018最新行政區域代碼

2.將下載好代碼,存入 mysql表中。如下表:

CREATE TABLE "public"."NewTable" (
"id" int4 NOT NULL,
"code" varchar(255) COLLATE "default",
"name" varchar(255) COLLATE "default",
"sort" int2,
"prentid" varchar COLLATE "default",
PRIMARY KEY ("id")
)

3.根據上述數據,再數據分層(上下級關係)

select * from test
where code like '%0000'

update test set sort=1,prentid=0
where code like '%0000'

---------------------
select left(code,2)||'0000' from test
where code like '%00' and sort is null

update test set sort=2,prentid=left(code,2)||'0000'
where code like '%00' and sort is null

---------------------------------
select *,left(code,4)||'00' from test
where code not like '%00' and sort is null

update test set sort=3,prentid=left(code,4)||'00'
where code not like '%00' and sort is null
-----------------------------------

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章