日期:2014-05-17  浏览次数:20489 次

求SQL高手帮忙呀!!!!
我这有两个表:实时数据表SECURITYPOSLOG,员工信息表:eminfo,我要根据卡号MSID分组取出最近一次的记录,本来下面的语句是没错的,但是因为SECURITYPOSLOG是不断实时不断插入的,导致有时候max(listmp)没有在临时表a中,最终导致查不出结果,求解,我该怎么做啊???
select 1 as type, * from  
(select maid,name, SECURITYPOSLOG.msid,max(listmp)as listmp  
 from SECURITYPOSLOG right join eminfo on eminfo.EMMSID=SECURITYPOSLOG.msid  
 where listmp>DATEADD(mi,-3,getdate()) group by SECURITYPOSLOG.msid,maid,name) a  
 where a.listmp in (select max(listmp)as listmp from SECURITYPOSLOG where listmp>DATEADD(mi,-3,getdate()) group by msid )  


------解决方案--------------------
1、读锁定,加with lock
2、如果要严格的读取最新的,lz需要利用串行读的隔离级别才可以,但同时就会带来锁定过多的问题。斟酌使用。