mysql根据身份证查询年龄、性别、出生日期

SELECT
	a.user_id,
IF
	(
		CHAR_LENGTH( idcard )< 18,(
			SUBSTR(now(),1,4)-(
			1900+SUBSTR(idcard,7,2)))-(
			SUBSTR(idcard,9,4)- date_format( now(), '%m%d')> 0 
			),(
			SUBSTR(now(),1,4)- SUBSTR(idcard,7,4))-(
			SUBSTR(idcard,11,4)- date_format(now(),'%m%d')> 0 
		)) AS age,
		cast(substring(idcard,7,8) as date) as 出生日期,
		case if(length(idcard)=18, cast(substring(idcard,17,1) as UNSIGNED)%2, if(length(idcard)=15,cast(substring(idcard,15,1) as UNSIGNED)%2,3))
  when 1 then '男'
  when 0 then '女'
  else '未知'
  end AS 性别,
	idcard 
FROM
	sys_user a INNER JOIN edu_retirement_manage b where a.user_id = b.user_id and  idcard is not null HAVING 1=1 and IF
	(
		CHAR_LENGTH( idcard )< 18,(
			SUBSTR(now(),1,4)-(
			1900+SUBSTR(idcard,7,2)))-(
			SUBSTR(idcard,9,4)- date_format( now(), '%m%d')> 0 
			),(
			SUBSTR(now(),1,4)- SUBSTR(idcard,7,4))-(
			SUBSTR(idcard,11,4)- date_format(now(),'%m%d')> 0 
		)) =  32 
		and 
		cast(substring(idcard,7,8) as date) =  '1990-03-07' 
		and 
	case if(length(idcard)=18, cast(substring(idcard,17,1) as UNSIGNED)%2, if(length(idcard)=15,cast(substring(idcard,15,1) as UNSIGNED)%2,3))   when 1 then '1'
  when 0 then '0'   else '未知'
  end = '1' 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值