56,678
社区成员
发帖
与我相关
我的任务
分享
id cardnumber pin type date
10 07007716 18 0 570129508
11 07007716 18 1 570129509
记录
id cardnumber pin eventtype date
1 00095367 19 0 567907199
2 06034084 0 27 570036777
3 07332724 0 27 570036783
4 07272196 0 27 570036787
5 07007716 18 0 570038464
6 05131445 17 0 570038467
7 00095367 19 0 570044486
8 07007716 18 0 570044511
9 00000000 0 206 570115150
10 07007716 18 0 570129508
11 07007716 18 20 570129509
12 07007716 18 20 570129510
13 07007716 18 0 570129514
查询
SELECT
a.ID,
a.CardNumber,
a.PIN,
a.ClassName,
a.VerifyType,
a.DoorId,
a.EventType,
a.State,
a.Date
FROM
recorders AS a,
recorders AS b
WHERE
a.ID = b.ID - 1
AND a.PIN = b.PIN
AND a.Date - b.Date <= 2
AND (
a.EventType = 0
)
结果
id cardnumber pin eventtype date
10 07007716 18 0 570129508
目前测试数据比较少,不过现在这样看差不多是对的了start transaction;
update recorders as a
set a.EventType = {0}, a.Date = {1}
where
a.PIN = {2}
and a.CardNumber = {3}
and a.ClassName = {4}
and ({5} - a.Date) <= 2
and (
a.EventType = 0
or a.EventType = 20
)
;
insert recorders select * from( select {0} as EventType, {1} as Date ) x where row_count() = 0;
commit;
update recorders as a
set a.EventType = {0}, a.Date = {1}
where
a.PIN = {2}
and a.CardNumber = {3}
and a.ClassName = {4}
and ({5} - a.Date) <= 2
and (
a.EventType = 0
or a.EventType = 20
)
@zjcxc 最后是这样处理的,在插入记录之前先update,根据影响行数决定是否再insert,这应该是你的意思吧?
目前不知道是手残还是怎么滴,好像还有update错误的记录,但是又好像没有,只能祈祷这段sql是万分正确的吧[/quote]
updae 和 insert 放在一个事务中,这样才能确保并发不会导致重复数据[/quote]
那意思是还用insert into on duplicate key update?不过这里可没有唯一索引了,除了id没有可以唯一的字段
另,一个事务啥意思?
update recorders as a
set a.EventType = {0}, a.Date = {1}
where
a.PIN = {2}
and a.CardNumber = {3}
and a.ClassName = {4}
and ({5} - a.Date) <= 2
and (
a.EventType = 0
or a.EventType = 20
)
@zjcxc 最后是这样处理的,在插入记录之前先update,根据影响行数决定是否再insert,这应该是你的意思吧?
目前不知道是手残还是怎么滴,好像还有update错误的记录,但是又好像没有,只能祈祷这段sql是万分正确的吧[/quote]
updae 和 insert 放在一个事务中,这样才能确保并发不会导致重复数据
update recorders as a
set a.EventType = {0}, a.Date = {1}
where
a.PIN = {2}
and a.CardNumber = {3}
and a.ClassName = {4}
and ({5} - a.Date) <= 2
and (
a.EventType = 0
or a.EventType = 20
)
@zjcxc 最后是这样处理的,在插入记录之前先update,根据影响行数决定是否再insert,这应该是你的意思吧?
目前不知道是手残还是怎么滴,好像还有update错误的记录,但是又好像没有,只能祈祷这段sql是万分正确的吧