计算年龄和月份的年龄

时间:2013-09-12 06:07:22

标签: mysql sql date

我在mysql中使用FROM_DAYS函数时出现问题,我想从my_table中获取年龄,这是我的查询;

select dob,CURRENT_DATE,
DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE,dob)),'%y yr %c mth %e dy') AS age, 
DATEDIFF(CURRENT_DATE,dob) days from my_table

结果:

dob       || CURRENT_DATE || age              || days  ||
==========++==============++==================++=======++
1953-09-10|| 2013-09-12   || 60 yr 1 mth 3 dy || 21917 ||
2013-09-08|| 2013-09-12   || 00 yr 0 mth 0 dy ||     4 ||

当我第二行尝试FROM_DAYS(DATEDIFF(CURRENT_DATE,m.tgllahir))时,结果为0000-00-00

然后我尝试查看Date and Time Functions并找到了这个

SELECT FROM_DAYS(730669);
        -> '2007-07-03'

但是当我尝试这是我得到的;

SELECT FROM_DAYS(730669);
        -> '2000-07-03'

我很好奇为什么FROM_DAYS功能不正常,但我的主要问题是如何找到年龄?

修改

与Barmar讨论后我知道我不能使用FROM_DAYS来获得准确的年龄,所以我试图找到不同的方法

4 个答案:

答案 0 :(得分:1)

要使用FROM_DAYS()函数并以您要查找的格式获取此人的年龄,您只需添加一个函数即可获得这样的年月或日期

CONCAT(
  YEAR(FROM_DAYS(DATEDIFF(NOW(),*DB LOCATION*))),' years,',
  MONTH(FROM_DAYS(DATEDIFF(NOW(),*DB LOCATION*))),' months,',
  DAY(FROM_DAYS(DATEDIFF(NOW(),*DB LOCATION*))),' days,',)

答案 1 :(得分:0)

这是我用来计算年,月,日的年龄的查询:

SELECT 
username
,dob
,DATE_FORMAT(CURDATE(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(CURDATE(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS years
,PERIOD_DIFF( DATE_FORMAT(CURDATE(), '%Y%m') , DATE_FORMAT(dob, '%Y%m') ) AS months
,DATEDIFF(CURDATE(),dob) AS days
FROM users

答案 2 :(得分:0)

SELECT FLOOR(( DATE_FORMAT(NOW(),'%Y%m%d') - DATE_FORMAT(t.dob,'%Y%m%d'))/10000) AS years,
       FLOOR((1200 + DATE_FORMAT(NOW(),'%m%d') - DATE_FORMAT(t.dob,'%m%d'))/100) %12 AS months,
       (SIGN(DAY(NOW()) - DAY(t.dob))+1)/2 * (DAY(NOW()) - DAY(t.dob)) +
      (SIGN(DAY(NOW()) - DAY(t.dob))+1)/2 * (DAY(NOW()) - DAY(t.dob)) +
      (SIGN(DAY(t.dob) - DAY(NOW()))+1)/2 * (DAY(STR_TO_DATE(DATE_FORMAT(t.dob + INTERVAL 1 MONTH,'%Y-%m-01'),'%Y-%m-%d') - INTERVAL 1 DAY) - DAY(t.dob) + DAY(NOW())) AS days
 FROM tablename t

尝试此查询

答案 3 :(得分:0)

我更喜欢使用功能。

这是我的功能的DDL。此功能准确,精确。

DELIMITER $$ 
DROP FUNCTION IF EXISTS f_age $$
CREATE FUNCTION `f_age`(
        x_dob DATE
    )
    RETURNS varchar(100) CHARSET latin1
    NOT DETERMINISTIC
    CONTAINS SQL

BEGIN
DECLARE x_result VARCHAR(100);
DECLARE y1, y2, m1, m2, d1, d2 INT;
DECLARE x1, x2 DATE;

  SET x_result = "";
  SET x1 = x_dob;
  SET x2 = CURDATE();

  IF (x1 < x2) THEN
    SET y1 = DATE_FORMAT(x1, '%Y');
    SET m1 = DATE_FORMAT(x1, '%c');
    SET d1 = DATE_FORMAT(x1, '%e');

    SET y2 = DATE_FORMAT(x2, '%Y');
    SET m2 = DATE_FORMAT(x2, '%c');
    SET d2 = DATE_FORMAT(x2, '%e');

    IF (d1 > d2) THEN
        SET m2 = m2 - 1;

        IF (m2 = 0) THEN
            SET y2 = y2 - 1;
            SET m2 = 12;
        END IF;

        SET d2 = d2 + DAY(LAST_DAY(CONCAT_WS("-", y2, m2, d2)));
    END IF;

    IF (m1 > m2) THEN
        SET y2 = y2 - 1;
        SET m2 = m2 + 12;
    END IF;

    IF (y2 > y1) THEN
        SET x_result = CONCAT((y2 - y1), ' yr ');
    END IF;

    IF ((y2 > y1) OR (m2 > m1)) THEN
        SET x_result = CONCAT(x_result, CONCAT((m2 - m1), ' mth '));
    END IF;

    IF ((y2 > y1) OR (m2 > m1) OR (d2 > d1)) THEN
        SET x_result = CONCAT(x_result, CONCAT((d2 - d1), ' dy'));
    END IF;
  ELSE
    SET x_result = "Error. Date of birth is today or more than today";
  END IF;

  RETURN x_result;
END $$

DELIMITER ;

比,这是使用函数的代码:

SELECT f_age(dob) FROM table;

它也准确处理闰年