douyu5679 2013-04-01 21:25
浏览 22
已采纳

最大员工小时的MySQL子查询

I've got a database table called "hours" which has the following fields: "employee_id" (INT), "time_on" (DATETIME), and "time_off" (DATETIME). I want to see which employee logged the most hours in a month.

I'm thinking I need a Subquery but I'm not very good with MySQL, and I cannot wrap my head around the query I need. I want to select all rows from this month with:

"... MONTH(time_on) = ".$this_month_object->format('m')."... "

Then with those rows, I want to select the "employee_id" field with the highest seconds worked using something like:

SUM(time_to_sec(TIMEDIFF(time_off, time_on)))

The last few hours have proved fruitless and I need some fresh ideas.

  • 写回答

1条回答 默认 最新

  • douwen9534 2013-04-01 21:27
    关注

    Something like this?

    SELECT employee_id, SUM(TIMEDIFF(time_off, time_on)) AS diff
    FROM tbl
    WHERE YEAR(time_on) = 2013
        AND MONTH(time_on) = 3
    GROUP BY employee_id
    

    See the demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度