我在MySQL中有两个表 - tickets
和ticket_updates
具有以下结构:
tickets
ticket_updates
关系是:tickets.ticketnumber = ticket_updates.ticketnumber
我想根据行插入ticket_updates
我尝试了这个查询:
SELECT avg(response_seconds) as s FROM
(SELECT time_to_sec(timediff(min(u.datetime), u.datetime)) AS response_seconds
FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber
WHERE u.type = 'update' and t.customer = 'Y' and DATE(u.datetime) = '2016-04-18'
GROUP BY t.ticketnumber)
AS r
但每次都会返回0.000
答案 0 :(得分:1)
我认为问题在于:
(SELECT time_to_sec(timediff(min(u.datetime), u.datetime)) AS response_seconds
在我的控制台上返回0。试试:
( select time_to_sec( timediff( from_unixtime( floor( UNIX_TIMESTAMP(u.datetime)/60 )*60 ), u.datetime) ) ) as response_seconds;
像
SELECT avg(response_seconds) as s FROM
( select time_to_sec( timediff( from_unixtime( floor( UNIX_TIMESTAMP(u.datetime)/60 )*60 ), u.datetime) ) ) as response_seconds
FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber
WHERE u.type = 'update' and t.customer = 'Y' and DATE(u.datetime) = '2016-04-18'
GROUP BY t.ticketnumber)
AS r