通过查询获得生日

时间:2018-03-31 19:28:36

标签: mysql

我在某些日期的年龄方面遇到了问题。

import pymysql
from DBUtils.SteadyDB import connect

db = connect(
  creator = pymysql, # the rest keyword arguments belong to pymysql
  user = 'guest', password = '', database = 'name', 
  autocommit = True, charset = 'utf8mb4', 
  cursorclass = pymysql.cursors.DictCursor)

结果是:

SELECT birthday, DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 AS age FROM users;

这次选择有什么不对?

2 个答案:

答案 0 :(得分:0)

这会有用吗?

SELECT TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS age FROM users;

答案 1 :(得分:-1)

您可以更好地使用此查询:

SELECT DATEDIFF(CURRENT_DATE, STR_TO_DATE(t.birthday, '%d-%m-%Y'))/365 AS ageInYears
  FROM YOUR_TABLE t