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 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库