有一个MySql表,记录身份证上的磁条刷卡次数:
CREATE TABLE `swipes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`personalid` varchar(20) NOT NULL,
`swipe_time` datetime NOT NULL,
`status` tinyint(1) NOT NULL COMMENT '1=registered,0=not',
`ride_taken` tinyint(1) NOT NULL DEFAULT '0',
`source` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `my_key` (`personalid`,`swipe_time`,`status`,`ride_taken`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8唯一的键在那里,以防止确切的重复插入。然而,还有另一种几乎重复的来源,在这种情况下,卡会在一分钟内连续刷卡两次,甚至会因为一些奇怪的原因而在一分钟左右之间刷卡。在某些方面,这就像电子开关触点上的弹跳。
下面是一些行的片段来说明:
id personalid swipe_time status ride_taken source
1661 C08877547 2012-10-21 01:12:08 1 1 3
1662 C09364782 2012-10-21 01:23:38 1 1 3
1663 C09364782 2012-10-21 01:23:48 1 1 3
1664 D09490557 2012-10-21 01:24:39 1 1 3我想要的是一个查询,该查询将删除id 1663这样的条目,这些条目在具有相同的personalid、status=1、ride_taken=1和源的指定时间内。
是的,我已经在这里和堆栈溢出(如https://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows )上搜索和检查过类似的情况,但是还没有找到答案。如果已经有了,请指给我看。
我不知道如何为swipe_time应用时间范围过滤器,以便在指定的时间范围内只删除类似的行。
发布于 2012-10-24 21:14:35
select id, personalid, swipe_time, status, ride_taken, source
from (
select *,
swipe_time2 := IF(@g=personalid AND
TIMESTAMPDIFF(SECOND, @swipe_time, swipe_time)<=10,
NULL, swipe_time),
@swipe_time := IF(@g=personalid AND
TIMESTAMPDIFF(SECOND, @swipe_time, swipe_time)<=10,
@swipe_time, swipe_time),
@g:=personalid
from (select @g:=null,@swipe_time:=null) a, swipes
order by personalid, swipe_time
) x
where swipe_time2 is not nullhttps://dba.stackexchange.com/questions/27554
复制相似问题