MySQL实现出生年月转年龄

描述及实现

表达函数

<math xmlns="http://www.w3.org/1998/Math/MathML"> f ( b i r t h d a y ) = { n o w D a t e − b i r t h d a y ( 岁 ) n o w D a t e − b i r t h d a y ≥ 1 年 n o w D a t e − b i r t h d a y ( 月 ) 1 年 > n o w D a t e − b i r t h d a y ≥ 1 月 n o w D a t e − b i r t h d a y ( 天 ) 1 月 > n o w D a t e − b i r t h d a y ≥ 0 天 f(birthday)=\begin{cases} nowDate-birthday (岁) & nowDate-birthday ≥ 1年 \\ nowDate-birthday (月) & 1年 > nowDate-birthday ≥ 1月 \\ nowDate-birthday (天) & 1月 >nowDate-birthday ≥ 0天 \\ \end{cases} </math>f(birthday)=⎩ ⎨ ⎧nowDate−birthday(岁)nowDate−birthday(月)nowDate−birthday(天)nowDate−birthday≥1年1年>nowDate−birthday≥1月1月>nowDate−birthday≥0天

  • nowDate:现在日期
  • birthday:出生年月日

SQL代码

sql 复制代码
select
	case
		when TIMESTAMPDIFF(year,'birthday',CURDATE()) != 0 
			then CONCAT(TIMESTAMPDIFF(year, 'birthday', CURDATE()), '岁')
		when TIMESTAMPDIFF(month,'birthday',CURDATE()) != 12 * TIMESTAMPDIFF(year,'birthday',CURDATE()) 
			then CONCAT(TIMESTAMPDIFF(month, 'birthday', CURDATE())-12 * TIMESTAMPDIFF(year, 'birthday', CURDATE()), '月')
		else CONCAT(ABS(TIMESTAMPDIFF(day, 'birthday', CURDATE())), '天')
	end as age,
from
	dual;

实践及效果

示例代码

sql 复制代码
select
	CURDATE() as currentDate,
	date_format('2023-1-14','%Y-%m-%d') as useYear, 
	case
		when TIMESTAMPDIFF(year,'2023-1-14',CURDATE()) != 0 
			then CONCAT(TIMESTAMPDIFF(year, '2023-1-14', CURDATE()), '岁')
		when TIMESTAMPDIFF(month, '2023-1-14', CURDATE()) != 12*TIMESTAMPDIFF(year, '2023-1-14', CURDATE()) 
			then CONCAT(TIMESTAMPDIFF(month, '2023-1-14', CURDATE())-12*TIMESTAMPDIFF(year, '2023-1-14', CURDATE()), '月')
		else CONCAT(ABS(TIMESTAMPDIFF(day, '2023-1-14', CURDATE())), '天')
	end as diffYear,
	date_format('2023-12-14','%Y-%m-%d') as useMonth, 
	case
		when TIMESTAMPDIFF(year ,'2023-12-14',CURDATE()) != 0 
			then CONCAT(TIMESTAMPDIFF(year, '2023-12-14', CURDATE()), '岁')
		when TIMESTAMPDIFF(month, '2023-12-14', CURDATE()) != 12*TIMESTAMPDIFF(year, '2023-12-14', CURDATE()) 
			then CONCAT(TIMESTAMPDIFF(month, '2023-12-14', CURDATE())-12*TIMESTAMPDIFF(year, '2023-12-14', CURDATE()), '月')
		else CONCAT(ABS(TIMESTAMPDIFF(day, '2023-12-14', CURDATE())), '天')
	end as diffMonth,
	date_format('2024-1-14','%Y-%m-%d') as useDay, 
	case
		when TIMESTAMPDIFF(year,'2024-1-14',CURDATE()) != 0 
			then CONCAT(TIMESTAMPDIFF(year, '2024-1-14', CURDATE()), '岁')
		when TIMESTAMPDIFF(month, '2024-1-14', CURDATE()) != 12*TIMESTAMPDIFF(year, '2024-1-14', CURDATE()) 
			then CONCAT(TIMESTAMPDIFF(month, '2024-1-14', CURDATE())-12*TIMESTAMPDIFF(year, '2024-1-14', CURDATE()), '月')
		else CONCAT(ABS(TIMESTAMPDIFF(day, '2024-1-14', CURDATE())), '天')
	end as diffDay
from
	dual;

示例效果

使用总结

对于涉及到人的年龄表来说,由于年龄会随着时间变化而变化,一般不合适直接存储年龄字段。所以就需要通过存储的出生年月日,进行动态计算。

相关推荐
小小虫码12 小时前
MySQL和Redis的区别
数据库·redis·mysql
苏-言12 小时前
Linux环境下的Java项目部署技巧:安装 Mysql
linux·运维·mysql
飞翔的佩奇14 小时前
Java项目: 基于SpringBoot+mybatis+maven+mysql实现的图书管理系统(含源码+数据库+答辩PPT+毕业论文)
java·数据库·spring boot·mysql·spring·毕业设计·图书管理
lwprain17 小时前
springboot 2.7.6 security mysql redis jwt配置例子
spring boot·redis·mysql
字节全栈_BjO20 小时前
mysql死锁排查_mysql 死锁问题排查
android·数据库·mysql
是小崔啊1 天前
事务03之MVCC机制
数据库·mysql·事务·
fajianchen1 天前
MySQL 索引存储结构
数据库·mysql
xianwu5431 天前
反向代理模块jmh
开发语言·网络·数据库·c++·mysql
geovindu2 天前
neo4j-community-5.26.0 create new database
数据库·mysql·neo4j
MyY_DO2 天前
maven mysql jdk nvm node npm 环境安装
java·mysql·maven