0

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

4

2 回答 2

2

试试这样...

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

编辑从交易查理考虑事实....

 SELECT
        id,
        Name,
        DateofBirth,
        TIMESTAMPDIFF(Year, Cast(DateofBirth as Date),
        Cast((Select Now()) as Date)) AS DiffDate
    from TABEL1
于 2013-06-17T11:15:45.460 回答
2

存储过程示例:

DATEDIFF(datepart, startdate, enddate)

更轻松:

-- Declare Two DateTime Variable
Declare @Date1 datetime 
Declare @Date2 datetime 
-- Set @Date1 with Current Date
set @Date1 = (SELECT GETDATE());
-- Set @Date2 with 5 days more than @Date1
set @Date2 = (SELECT DATEADD(day, 5,@Date1 ))
-- Get The Date Difference
SELECT DATEDIFF(day, @Date1, @Date2) AS DifferenceOfDay
于 2013-06-17T11:22:04.970 回答