Grails - MySQL查询导致错误:时间格式不正确

时间:2012-05-17 07:20:38

标签: mysql grails gorm

我有这个查询(在mySQL cmdline中运行正常),但是当我使用executeQuery在grails中运行它时,我收到此错误:时间格式错误

这是我的grails查询:

def aveTimeToClose = OstFacTicket.executeQuery
("SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(t.closed, t.created)))) as cl " +
"from OstFacTicket t WHERE t.created >= :sdate AND t.created <= :edate AND " +
"t.closed IS NOT NULL", [sdate:start, edate: end])

堆栈跟踪错误是:

 Error 2012-05-17 00:09:44,356 [http-bio-8080-exec-9] 
ERROR util.JDBCExceptionReporter
- Bad format for Time '187:22:05' in column 1
| Error 2012-05-17 00:09:44,365 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver  

在mySQL cmdline上运行可以正常工作:

mysql> SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(t.closed, t.created)))) as cl
from ost_fac_ticket t where created > '2011-01-01 08:12:49' AND created < 
'2011-12-31 10:12:49';
+-----------+
| cl        |
+-----------+
| 187:22:05 |
+-----------+

谢谢,我感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我最后关注mu is too short的建议,只需在几秒钟内获取查询结果,然后使用groovy将秒数转换为我想要的格式。

基本上 -

def aveTimeToClose = OstFacTicket.executeQuery("SELECT AVG(TIME_TO_SEC(TIMEDIFF(t.closed, t.created))) as cl "+
        "from OstFacTicket t WHERE t.created >= :sdate AND t.created <= :edate AND t.closed IS NOT NULL", [sdate:start, edate: end])

结果aveTimeToClose以秒为单位,可以很容易地转换为日期时间格式。