欢迎投稿

今日深度:

mysql精确查年龄,

mysql精确查年龄,


已知出生年月日,求到今天为止多少岁


select *,
	--如果当前月份大于出生月,年龄 =  当前年份 - 出生年
	if (month(current_date())-month(substr(id_card,7,8))>0,
	year(current_date())-year(substr(id_card,7,8)),
	--如果当前月份小于出生月,年龄 =  当前年份 - 出生年 - 1
	if(
	month(current_date())-month(substr(id_card,7,8))<0,
	year(current_date())-year(substr(id_card,7,8))-1,
	--如果当前月份等于出生月,比较日期
	if(
	--当前日期大于出生日期 ,年龄 =  当前年份 - 出生年
	day(current_date())-day(substr(id_card,7,8))>0,
	year(current_date())-year(substr(id_card,7,8)),
	---当前日期小于出生日期 ,年龄 =  当前年份 - 出生年 - 1
	year(current_date())-year(substr(id_card,7,8))-1	
)
)	
)as 'age'
from person limit 10;

本文来自博客园,作者:JD_L,转载请注明原文链接:https://www.cnblogs.com/dream-come-true/p/17287916.html

www.htsjk.Com true http://www.htsjk.com/Mysql/46058.html NewsArticle mysql精确查年龄, 已知出生年月日,求到今天为止多少岁 select *,--如果当前月份大于出生月,年龄 = 当前年份 - 出生年if (month(current_date())-month(substr(id_card,7,8))0,year(current_date())-year(subs...
评论暂时关闭