本文主要是介绍MySQL由身份证衍生出的统计数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
按地域分布统计:
select
elt(interval(left(id_number,2),11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82),'北京市','天津市','河北省','山西省','内蒙古自治区','辽宁省','吉林省',
'黑龙江省','上海市','江苏省','浙江省','安徽省','福建省','江西省','山东省','河南省','湖北省','湖南省','广东省','广西壮族自治区','海南省','重庆市','四川省','贵州省','云南省','西藏自治区',
'陕西省','甘肃省','青海省','宁夏回族自治区','新疆维吾尔自治区','台湾省','香港特别行政区','澳门特别行政区')
as regions,count(id) as counts from table group by elt(interval(left(id_number,2),11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82),'北京市','天津市','河北省','山西省','内蒙古自治区','辽宁省','吉林省',
'黑龙江省','上海市','江苏省','浙江省','安徽省','福建省','江西省','山东省','河南省','湖北省','湖南省','广东省','广西壮族自治区','海南省','重庆市','四川省','贵州省','云南省','西藏自治区',
'陕西省','甘肃省','青海省','宁夏回族自治区','新疆维吾尔自治区','台湾省','香港特别行政区','澳门特别行政区');
按性别统计:
select elt(interval(a.gender,0,1),'female','male') as genders,count(a.uid) as counts from (
select if(length(id_number)=18, cast(substring(id_number,17,1) as unsigned)%2, if(length(id_number)=15, cast(substring(id_number, 15 ,1) as unsigned)%2,3)) as gender,id as uid
from table) a group by elt(interval(a.gender,0,1),'female','male');
按年龄统计:(此处是按0-18岁,18到30岁,30岁到50岁,50岁及以上分组)
select elt(interval(a.age,0,18,30,50),'less18','18to30','30to50','more50') as user_age,count(a.uid) as counts from (
select year(curdate())-if(length(id_number)=18,substring(id_number, 7, 4),if(length(id_number)=15, concat('19',substring(id_number, 7, 2)),null)) as age,id as uid
from table) a group by elt(interval(a.age,0,18,30,50),'less18','18to30','30to50','more50');
注:由身份证统计,身份证号必须正确,特殊群体的身份证有的无法查询出结果。
这篇关于MySQL由身份证衍生出的统计数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!