2015-06-30 89 views
0

我陷入了問題。我在mysql數據庫中有一張表,其中我有一些員工在特定位置的出勤記錄。我想計算特定日期的員工的總登錄時間。 這是我的表格的圖像。格式化抱歉。計算mysql中兩行之間的時間差有非連續的ID

enter image description here

+0

用戶,地點,日期,時間,狀態定購。然後做時間計算。用戶可能在同一地點登錄兩次,或者不登出? –

+0

用戶可以在一天中登錄多次,但他無法登出而無法登出。即登錄和註銷是連續的條目。 – user2576232

+0

' - '是什麼意思?和日期/時間應該是一個單一的實體 – Strawberry

回答

0

在SQL這是困難的,因爲你沒有一種簡單的方法來登錄&註銷記錄匹配在一起。

對於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'