MySQL 根据身份证号查询个人信息

select  
-- 身份证号的前两位是省份
case left(zjhm,2) 
when '11' then '北京市'
when '12' then '天津市'
when '13' then '河北省'
when '14' then '山西省'
when '15' then '内蒙古自治区'
when '21' then '辽宁省'
when '22' then '吉林省'
when '23' then '黑龙江省'
when '31' then '上海市'
when '32' then '江苏省'
when '33' then '浙江省'
when '34' then '安徽省'
when '35' then '福建省'
when '36' then '江西省'
when '37' then '山东省'
when '41' then '河南省'
when '42' then '湖北省'
when '43' then '湖南省'
when '44' then '广东省'
when '45' then '广西壮族自治区'
when '46' then '海南省'
when '50' then '重庆市'
when '51' then '四川省'
when '52' then '贵州省'
when '53' then '云南省'
when '54' then '西藏自治区'
when '61' then '陕西省'
when '62' then '甘肃省'
when '63' then '青海省'
when '64' then '宁夏回族自治区'
when '65' then '新疆维吾尔自治区'
when '71' then '台湾省'
when '81' then '香港特别行政区'
when '82' then '澳门特别行政区'
else '其他'     
end   as 省份 ,

-- 截取身份证号的前四位就得到地市,这里以山东省为例
case left(zjhm,4) 
when '3701' then '山东省济南市'
when '3702' then '山东省青岛市'
when '3703' then '山东省淄博市'
when '3704' then '山东省枣庄市'
when '3705' then '山东省东营市'
when '3706' then '山东省烟台市'
when '3707' then '山东省潍坊市'
when '3708' then '山东省济宁市'
when '3709' then '山东省泰安市'
when '3710' then '山东省威海市'
when '3711' then '山东省日照市'
when '3712' then '山东省莱芜市'
when '3713' then '山东省临沂市'
when '3714' then '山东省德州市'
when '3715' then '山东省聊城市'
when '3716' then '山东省滨州市'
when '3717' then '山东省菏泽市'
else '其他'     
end as 地市,


-- 截取身份证号的前六位就得到区县,这里以山东省济南市为例(行政区划可能会调整,以实际为准)
case left(zjhm,6) 
when '370102' then '山东省济南市历下区'
when '370103' then '山东省济南市市中区'
when '370104' then '山东省济南市槐荫区'
when '370105' then '山东省济南市天桥区'
when '370112' then '山东省济南市历城区'
when '370113' then '山东省济南市长清区'
when '370114' then '山东省济南市章丘区'
when '370115' then '山东省济南市济阳区'
when '370116' then '山东省济南市莱芜区'
when '370117' then '山东省济南市钢城区'
when '370124' then '山东省济南市平阴县'
when '370126' then '山东省济南市商河县'
else '其他'     
end as 区县,

-- 出生日期
IF (LENGTH(zjhm) = 18,CAST(SUBSTRING(zjhm, 7, 8) AS date),
    CAST(CONCAT('19',SUBSTRING(zjhm, 7, 6)) AS date) ) AS 出生日期,


-- 年龄
year(curdate())-if(length(zjhm)=18,substring(zjhm,7,4),if(length(zjhm)=15,concat('19',substring(zjhm,7,2)),null)) as 年龄, 
 
-- 性别
case if(length(zjhm)=18, cast(substring(zjhm,17,1) as UNSIGNED)%2, if(length(zjhm)=15,cast(substring(zjhm,15,1) as UNSIGNED)%2,3)) 
when 1 then '男'
when 0 then '女'
else '未知'
end as 性别
 
FROM `cs_id_card`

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

互联网学徒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值