如何在sql

时间:2015-09-21 09:39:30

标签: mysql sql

我有一张表格如下

number         Timestamp  
123456         17-09-2015 11:06 
455677         17-09-2015 11:09
123456         17-09-2015 11:10  
453377         17-09-2015 11:20
123456         17-09-2015 11:35
123456         17-09-2015 11:42  

结果应如下:

123456         17-09-2015 11:06
123456         17-09-2015 11:10 

每小时,比如从11:00到12:00,我搜索在5分钟内出现重复的数字。我只知道一些非常基本的sql命令。我试图在一小时内获得重复的行。

从my_table t中选择t.number,group_concat(时间),其中t.time> = now() - 间隔1小时和t.time< = now()组由t.number具有count(*)> 1; 但是每5小时内在5分钟内找到重复记录对我来说似乎太复杂了。什么sql语句可以做到这一点?提前谢谢。

1 个答案:

答案 0 :(得分:4)

以下查询将为您提供每5分钟间隔重复number的列表:

select number,group_concat(date_time)
from
t1
group by floor(unix_timestamp(date_time)/(5 * 60)),number having count(*)>1;

http://sqlfiddle.com/#!9/75ea0/1

验证结果
Group 1: 00:00:00 to 00:04:59
Group 2: 00:05:00 to 00:09:59
and so on..for equal duration of 5 minutes each