使用SQL从类型日期的生日字段获取年龄

编程入门 行业动态 更新时间:2024-10-26 20:22:16
本文介绍了使用SQL从类型日期的生日字段获取年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在MySQL数据库中有一个表,其中包含 date 类型的字段。这里存储用户的生日。是否有一种纯SQL方式来从字段中选择年龄?

I have a table in a MySQL database with a field of the type date. Herein the birthday of a user is stored. Is there a pure SQL way to select the age in years from the field?

推荐答案

尝试这样...

SELECT id, Name, DateofBirth, ((DATEDIFF(Cast((Select Now()) as Date), Cast(DateofBirth as Date)))/365) AS DiffDate from TABEL1

编辑从Transact Charlie ...了解事实...

Edit Considereing Fact from Transact Charlie....

SELECT id, Name, DateofBirth, TIMESTAMPDIFF(Year, Cast(DateofBirth as Date), Cast((Select Now()) as Date)) AS DiffDate from TABEL1

更多推荐

使用SQL从类型日期的生日字段获取年龄

本文发布于:2023-10-18 11:49:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   年龄   日期   类型   生日

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!