doubo3384 2014-03-07 05:26
浏览 120
已采纳

检索两个日期之间的不同小时数

I would appreciate it if someone could help me with some logic here on how to turn my timestamp function into a working payroll service.

Right now when a user logs in to my site, they can click a clock in button which timestamps the datetime format into SQL.

This is the format: 2013-05-12 09:19:00

  • On click, it starts the clock in the in column.
  • When they click again, it adds to the out column.

It is storing in these columns:

  • id (auto_increment)
  • userid (unique)
  • in (datetime)
  • out (datetime)

How would a function look / work to grab total hours clocked from an employee?

At the least, point me in the right direction to resolve this scenario please.

  • 写回答

3条回答 默认 最新

  • dshakcsq64956 2014-03-07 05:46
    关注

    You can take advantage of the MySQL functions SUM and TIMESTAMPDIFF to calculate it for you:

    SELECT SUM(TIMESTAMPDIFF(HOUR, `in`, `out`)) AS hours
      FROM myTable
     WHERE userid = 1
    

    Live DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大