mysql 2018最新行政代码导入方法(包括递级关系)

本文介绍了一种简单的方法,用于将民政部发布的2018年最新行政区域代码导入MySQL数据库,并通过SQL语句建立上下级关系,实现数据分层。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单方便方法如下:

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
-----------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小努蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值