使用SQL从类型日期的生日字段获取年龄 [英] Get age from the birthday field with type date using SQL

查看:951
本文介绍了使用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从类型日期的生日字段获取年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆