我在某些日期的年龄方面遇到了问题。
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;
这次选择有什么不对?
答案 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