MYSQL-无法计算到字段之间的timediff并且无法使用where子句

时间:2016-01-27 17:08:41

标签: mysql

这是用于计算DateTimeSolved和DateTimeCreated之间的时差的SQL查询,我需要结果以小时为单位。

$query = "SELECT problem.id, problem.DateTimeCreated, problem.DateTimeSolved TIMEDIFF(HOUR, problem.DateTimeSolved, problem.DateTimeCreated)
From problem";

我已将where子句添加到我的查询中:

$ query =“SELECT problem.status,problem.id,problem.DateTimeSolved,problem.DateTimeCreated,TIMESTAMPDIFF(DAY,problem.DateTimeSolved,problem.DateTimeCreated) 从问题 WHERE problem.status ='5' GROUP BY MONTH(problem.DateTimeCreated)= MONTH(CURDATE())“;这是它工作的查询的当前格式,但我得到的输出是否定的。

当我尝试运行语句时,我仍然收到此错误:

  

您的SQL语法有错误;

     

检查手册   对应于您的MySQL服务器版本以获得正确的语法

3 个答案:

答案 0 :(得分:0)

你错过了一个逗号而且timediff函数只需要两个值,要格式化它(只显示小时)使用以下查询:

SELECT problem.id, problem.DateTimeCreated, problem.DateTimeSolved, TIME_FORMAT(TIMEDIFF(problem.DateTimeSolved, problem.DateTimeCreated), '%k %i')
From problem

答案 1 :(得分:0)

错误消息是由于在TIMEDIFF函数之前缺少逗号:

SELECT problem.id, problem.DateTimeCreated, problem.DateTimeSolved, TIMEDIFF(HOUR, problem.DateTimeSolved, problem.DateTimeCreated)

从问题

然后您会发现TIMEDIFF的参数不正确。这应该有效:

SELECT problem.id, problem.DateTimeCreated, problem.DateTimeSolved, TIMEDIFF( problem.DateTimeSolved, problem.DateTimeCreated)
From problem

如果您只需要几个小时:

SELECT problem.id, problem.DateTimeCreated, problem.DateTimeSolved, HOUR(TIMEDIFF( problem.DateTimeSolved, problem.DateTimeCreated)) From problem

答案 2 :(得分:-1)

试试这个

$query = "SELECT problem.id, problem.DateTimeCreated, problem.DateTimeSolved TIMESTAMPDIFF(HOUR, problem.DateTimeSolved, problem.DateTimeCreated)
From problem";