mysql工作时间获取,在一天mysql中获取总工作时间

I have a MySQL table where employee login and logout timings are recorded. Here in the in-out column 1-represents login and 0-represents logout.

[id] [User_id] [Date_time] [in_out]

1 1 2011-01-20 09:30:03 1

2 1 2011-01-20 11:30:43 0

3 1 2011-01-20 11:45:12 1

4 1 2011-01-20 12:59:56 0

5 1 2011-01-20 13:33:11 1

6 1 2011-01-20 15:38:16 0

7 1 2011-01-20 15:46:23 1

8 1 2011-01-20 17:42:45 0

Is it possible to retrieve total hours worked in a day by a user using single query?

I tried a a lot but all in vain. I can do this in PHP using array but unable to do so using single query.

解决方案SELECT `User_id`, time(sum(`Date_time`*(1-2*`in_out`)))

FROM `whatever_table` GROUP BY `User_id`;

The (1-2*`in_out`) term gives every login event a -1 factor and every logout event a +1 factor. The sum function takes the sum of the Date_time column, and GROUP BY `User_id` makes that the sum for each different user is created.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值