简体   繁体   English

计算具有非连续ID的mysql中两行之间的时间差

[英]calculate time difference between two rows in mysql having non consecutive ids

I am stuck in problem. 我陷入了问题。 I have a table in mysql database, in which I have attendance record of some employees at particular locations.I want to calculate total login time for an employee on specific date. 我在mysql数据库中有一个表,其中有一些特定位置的员工的出勤记录。我想计算特定日期某个员工的总登录时间。 Here is the image of my table. 这是我桌子的图像。 Sorry for formatting. 抱歉,无法格式化。

在此处输入图片说明

In sql this is difficult, since you don't have a simple way to match login&logout records together. 在sql中,这很困难,因为您没有简单的方法将登录和注销记录匹配在一起。

For SQL it would go like this (Not tested) 对于SQL,它将像这样(未经测试)

select t1.*, t1.date+t1.time as LoginTimeStamp,
 (select top 1 t2.date+t2.time from table t2 
    where 
         t1.user_id=t2.user_id 
     and t1.location_id=t2.location_id
     and t2.date+t2.time > t1.date+t1.time 
    order by t2.date+t2.time asc) 
 as LogoutTimeStamp
 from table t1 where attendance_status='In'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM