本文主要记录了 中华人民共和国 国家行政区划单位数据的查询和统计常用sql语句

数据来源

本示例中使用的数据来自国家民政部发布的《2019年5月中华人民共和国县以上行政区划代码》

www.mca.gov.cn/article/sj/xzqh/2019/

民政部对国家行政区划每月进行更新和调整。

查询代码(mysql)

-- 直辖市数量

select count(*) from xzqh201905 where SUBSTR(code,3,4)='0000' and instr(name,'市')>0;

-- 直辖市清单

select * from xzqh201905 where SUBSTR(code,3,4)='0000' and instr(name,'市')>0;

-- 省级行政区划单位数量

select count(*) from xzqh201905 where SUBSTR(code,3,4)='0000';

-- 省级行政区划单位清单

select * from xzqh201905 where SUBSTR(code,3,4)='0000';

-- 地级市数量

select count(*) from xzqh201905 where SUBSTR(code,3,4)!='0000' and SUBSTR(code,5,2)='00' and instr(name,'市')>0;

-- 地级市清单

select nlt.upcode as pro_code , plt.name as pro_name,nlt.code,nlt.name from (select CONCAT(substr(code,1,2),'0000') as upcode, code,name from xzqh201905 where SUBSTR(code,3,4)!='0000' and SUBSTR(code,5,2)='00' and instr(name,'市')>0) nlt ,(select * from xzqh201905 where SUBSTR(code,3,4)='0000') as plt where plt.code =nlt.upcode;;

-- 地级行政区划单位数量

select count(*) from xzqh201905 where SUBSTR(code,3,4)!='0000' and SUBSTR(code,5,2)='00' ;

-- 地级行政区划单位清单

select * from xzqh201905 where SUBSTR(code,3,4)!='0000' and SUBSTR(code,5,2)='00' ;

-- 县级市数量数量

select count(*) from xzqh201905 where SUBSTR(code,5,2)!='00' and instr(name,'市')>0;

-- 县级市清单

select * from xzqh201905 where SUBSTR(code,5,2)!='00' and instr(name,'市')>0;

-- 各省行地级政区划数量统计

select plt.name,nlt.count,nlt.list from (select CONCAT(substr(lt.code,1,2),'0000') as code , count(*) as count,GROUP_CONCAT(name) as list from xzqh201905 lt where substr(lt.code,3,2)!=00 and substr(lt.code,5,2)='00' group by substr(lt.code,1,2)) as nlt,(select * from xzqh201905 where SUBSTR(code,3,4)='0000') as plt where plt.code =nlt.code;

-- 各省县级市行政区划数量统计

select plt.name,nlt.count,nlt.list from (select CONCAT(substr(lt.code,1,2),'0000') as code , count(*) as count,GROUP_CONCAT(name) as list from xzqh201905 lt where substr(lt.code,3,2)!=00 and substr(lt.code,5,2)!='00' and instr(lt.name,'市')>0 group by substr(lt.code,1,2)) as nlt,(select * from xzqh201905 where SUBSTR(code,3,4)='0000') as plt where plt.code =nlt.code;

-- 三级行政区划清单

select nlt.pro_code as pro_code, plt.name as pro_name, nlt.dis_code as dis_code,nlt.dis_name as dis_name,'' as code,'' as name from (select CONCAT(substr(code,1,2),'0000') as pro_code , name as pro_name, code as dis_code,name as dis_name from xzqh201905 where SUBSTR(code,1,2) in ('11','12','31','50') and SUBSTR(code,3,2)!='00') as nlt,(select * from xzqh201905 where SUBSTR(code,3,4)='0000') as plt where nlt.pro_code=plt.code

UNION ALL

select nlt.pro_code as pro_code, plt.name as pro_name, nlt.dis_code as dis_code,dlt.name as dis_name,nlt.code,nlt.name from (select CONCAT(substr(code,1,2),'0000') as pro_code,CONCAT(substr(code,1,4),'00') as dis_code,code,name from xzqh201905 where SUBSTR(code,5,2)!='00') as nlt,(select * from xzqh201905 where SUBSTR(code,3,4)!='0000' and SUBSTR(code,5,2)='00') as dlt,(select * from xzqh201905 where SUBSTR(code,3,4)='0000') as plt where nlt.pro_code=plt.code and nlt.dis_code=dlt.code;

-- 三级行政区划树

select '' as parent_code,'000000' as code ,'全国行政区划' as name

UNION all

select if(SUBSTR(code,3,4)='0000','000000',if(SUBSTR(code,5,2)='00',CONCAT(SUBSTR(code,1,2),'0000'),if(SUBSTR(code,1,2) in ('11','12','31','50'),CONCAT(SUBSTR(code,1,2),'0000'),CONCAT(SUBSTR(code,1,4),'00')))) as parent_code,code,name from xzqh201905 ;

三级行政区划 mysql_全国三级行政区划数据 常用查询语句相关推荐

  1. MongoDB 查询语法与常用查询语句总结

    MongoDB 常用查询语句总结 先来一波查询语句语法的基本解释: 列子: db.mycol.find({"likes": {$gt:10}, $or: [{"by&qu ...

  2. Mongodb常用查询语句_笔记

    目录 前言 一.Mongodb简介 二.Mongodb常用查询语句 总结 前言 工作中会使用到Mongodb数据库,这是一个非关系型数据库,所以它的一些查询语句跟sql会不太一样,一时半会不能马上写出 ...

  3. SQL常用查询语句汇总

    SQL查询关键字为SELECT,常用查询语句代码及结果如下(本文使用MySQL数据库管理系统): -- 1检索单个列 SELECT prod_name FROM Products;-- 2检索多个列 ...

  4. 结构化查询语句简称mysql_整理MySql常用查询语句

    MySql的性能优化 性能优化是通过某些有效的方法提高MySQL数据库的性能.性能优化的目的是为了是MySQL数据运行速度更快.占用的磁盘空间更小.性能优化包括很多方面,例如优化查询速度.优化更新速度 ...

  5. mysql 常用查询语句

    常用的查询语句 数据库使用的是:mysql 8.0.28,安装过程可参考: mysql 8.0安装教程 1.初识SQL SQL是为操作数据库而开发的语言.国际标准化组织(ISO)为 SQL 制定了相应 ...

  6. Hive 表常用查询语句-总结

    Hive之前不常用,每次都是现用现查,就是现在总结记下笔记,边学边记(下面都是一些简单的例子,由易到难吗)>_<. 1.基本的查询语句 现在假设有数据库 db,数据表table1,tabl ...

  7. MySQL数据库的查询:常用查询语句、MySQL函数、多表查询、视图表

    文章目录 一.构造数据 二.常用的查询语句 1.SELECT:字段表达式 2.FROM 子句 3.WHERE 子句:按指定条件过滤 4.GROUP BY:分组查询 5.HAVING 6.ORDER B ...

  8. 数据库MySQL(一) 常用查询语句

    MySQL笔记 数据库表.查询.排序.数据处理函数 1 数据库 什么是数据库?什么是数据库管理系统?什么是SQL?它们之间的关系? 数据库: 顾名思义:存储数据的仓库,实际上就是一堆文件,这些文件中存 ...

  9. influxdb概念 常用查询语句

    一, 基础操作 # 创建数据库 create database <dataname> # 删除数据库 drop database <dataname> # 查看数据库 show ...

  10. MySQL SELECT:数据表查询语句

    MySQL 表单查询是指从一张表的数据中查询所需的数据,主要有查询所有字段.查询指定字段.查询指定记录.查询空值.多条件的查询.对查询结果进行排序等. MySQL SELECT 基本语法 MySQL ...

最新文章

  1. php qq对话,用php聊QQ
  2. 在python中、处理的一切都是对象_Python 3+ 一切都是对象
  3. 简明的后台样式查询模板
  4. Kali Linux 无线渗透测试入门指南 第八章 攻击企业级 WPA 和 RADIUS
  5. C语言和设计模式(工厂模式)
  6. CoreData 从入门到精通(三)关联表的创建
  7. 页面刷新_刷新vue页面,解决数据丢失
  8. AI数学手册:线性代数、拓扑、微积分和最优化 | 资料
  9. 高频电子线路复习笔记(2)——高频电路基础
  10. [TJOI2018]教科书般的亵渎
  11. 呼叫压力测试软件,MyComm呼叫中心压力测试解决方案
  12. 打印机显示脱机怎么办?
  13. 4k 显示器放大 150% 和 23寸显示器组双屏抓图问题解决
  14. 计算机视觉 | 八斗人工智能 (上)
  15. 设备安全--IPS部署与维护
  16. 咨询_计算机屏幕_雾面屏;
  17. Swift基础之封装一个WebViewController
  18. Qt5 编译错误找不到头文件的解决方法
  19. mmap函数使用实例
  20. go gin下配置https

热门文章

  1. 医疗器械软件网络安全测试报告,《医疗器械网络安全注册技术审查指导原则》延伸解读——网络安全可追溯分析报告...
  2. 自己整理的几个免费的李炎恢php实战开发教程
  3. 简单的安卓木马制作(实现外网控制)
  4. Windows下U盘无法格式化原因及解决办法:Windows无法完成格式化
  5. windows无法完成格式化U盘的几种终极解决办法
  6. sql server 2005 32位+64位、企业版+标准版、CD+DVD 下载地址大全【申明:来源于网络】
  7. 三星线刷工具Odin3_V3.12.7
  8. DirectShow 开启摄像头
  9. Flash CS6 新功能
  10. shopex admincore.php,shopex网店系统更换空间后出错:Fatal error: Incompatible file format:...