在MySQL中计算年龄时出错? [英] Error in calculating age in MySQL?

查看:161
本文介绍了在MySQL中计算年龄时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的SQL语句计算年龄

I am trying to calculate age with the SQL statement below

round(datediff(now() - dateofbirth / 365))

出现以下错误,

1582-调用本机函数'datediff'时参数计数错误

1582 - Incorrect parameter count in the call to native function 'datediff'

推荐答案

问题: datediff需要两个像DATEDIFF(expr1,expr2)这样的参数,根据给定的问题,只有一个参数,因此出错

Problem : datediff expects two parameters like DATEDIFF(expr1,expr2) , as per the given question, there is only one parameter hence error.

解决方案:,您必须使用

round(datediff(now() , (dateofbirth / 365)))
                     ^

datediff返回expression1 – expression2,因此您不必自己执行:)还要确保参数是日期或日期和时间表达式

the datediff returns expression1 – expression2, so you dont have to do it by yourself :) also make sure the parameters are date or date-and-time expressions

@Jonathan Leffler指出的另一个错误是在调用DATEDIFF之后需要除法.

EDIT : Another error pointed out by @Jonathan Leffler,division is needed after the call to DATEDIFF.

round(datediff(now(),dateofbirth)/365)

这篇关于在MySQL中计算年龄时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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