MySQL 根据身份证出生年月计算年龄户籍地性别

一、年龄

1、身份证判断日期:

SELECT  DATE_FORMAT(substring(replace([身份证号码],' ',''), 7, 8),"%Y-%m-%d") birthday ;

2、身份证判断年龄(精确到年月日)

SELECT TIMESTAMPDIFF(YEAR,DATE_FORMAT(substring(replace([身份证号码],' ',''), 7, 8),"%Y-%m-%d") , DATE_FORMAT( NOW(),"%Y-%m-%d")  )

3、年龄实例(未足天)

SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2003-04-25');

 4、年龄实例(足天)

SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2003-05-02');

二.所属省份:

CASE 
LEFT(`card_id`,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 '新疆' END AS 所属省份 

三.性别:

   IF (MOD(SUBSTRING(`card_id`,17,1),2),'男','女') AS 性别

 

你可能感兴趣的:(MySQL)