查询以显示缺少的时间间隔

时间:2015-07-21 06:54:16

标签: mysql database

我想显示每半小时和之间缺少时间间隔的结果。 我怎样才能实现这一目标? 这是我的问题:

SET @userid=8;
SET @start = '2015-07-20 08:00:00';
SET @end = '2015-07-20 17:00:00';
SELECT FROM_UNIXTIME((UNIX_TIMESTAMP(time_of_call) DIV (30*60)) *30*60) elapsed, COUNT( call_id ) value 

FROM calls 

WHERE
    calling_agent = @userid 
    AND time_of_call > @start 
    AND time_of_call < @end 
GROUP BY UNIX_TIMESTAMP(time_of_call) DIV (30*60)
ORDER BY time_of_call
DESC

1 个答案:

答案 0 :(得分:0)

您可以使用GROUP BY UNIX_TIMESTAMP(sub1.sdate) DIV (30*60)分组30分钟。

要显示广告位的开始时间,您可以使用FROM_UNIXTIME((UNIX_TIMESTAMP(sub1.sdate) DIV (30*60)) *30*60)

编辑问题编辑:

以下是匹配代码:

SELECT * FROM  calls
RIGHT JOIN (
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(@start)+(v1.v+v2.v+v3.v+v4.v+v5.v)*30*60) vv from
 (select 0 v union select 1) v1
 CROSS JOIN (select 0 v union select 2) v2
 CROSS JOIN (select 0 v union select 4) v3
 CROSS JOIN (select 0 v union select 8) v4
 CROSS JOIN (select 0 v union select 16) v5
) tx ON (FROM_UNIXTIME(UNIX_TIMESTAMP(time_of_call) DIV (30*60)*30*60)=vv)
WHERE  
 calls.calling_agent=@userid 
 AND calls.time_of_call IS NULL
 AND FROM_UNIXTIME(UNIX_TIMESTAMP(@start)+(v1.v+v2.v+v3.v+v4.v+v5.v)*30*60) < @end;