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;

示例效果

使用总结

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

相关推荐
用户8307196840827 小时前
Java 告别繁琐数据统计代码!MySQL 8 窗口函数真香
java·sql·mysql
stark张宇9 小时前
MySQL 核心内幕:从索引原理、字段选型到日志机制与外键约束,一篇打通数据库任督二脉
数据库·mysql·架构
Hoffer_1 天前
MySQL 强制索引:USE/FORCE INDEX 用法与避坑
后端·mysql
Hoffer_1 天前
MySQL 索引核心操作:CREATE/DROP/SHOW
后端·mysql
0xDevNull3 天前
MySQL索引进阶用法
后端·mysql
0xDevNull3 天前
MySQL索引用法
mysql
程序员小崔日记3 天前
一篇文章彻底搞懂 MySQL 和 Redis:原理、区别、项目用法全解析(建议收藏)
redis·mysql·项目实战
武子康3 天前
大数据-241 离线数仓 - 实战:电商核心交易数据模型与 MySQL 源表设计(订单/商品/品类/店铺/支付)
大数据·后端·mysql
用户8307196840824 天前
MySQL 查询优化 30 条封神技巧:用好索引,少耗资源,查询快到飞起
mysql
Nyarlathotep01134 天前
事务隔离级别
sql·mysql