首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql使用组的另一种方式

mysql使用组的另一种方式
EN

Stack Overflow用户
提问于 2014-12-05 14:50:29
回答 1查看 83关注 0票数 1

我在做一些时间匹配,但太花时间了。

我有个例子

表aaa

代码语言:javascript
复制
id | emp_id|shift_in               | shift_out             | actual_timein | actual_timout 
1  | 123   | "2014-10-11 01:00:00" | "2014-10-11 01:00:00" | null          | null

表bbb状态0=out : 1=in

代码语言:javascript
复制
id| emp_id | status | timelog | 
1 | 123    | 1      |  "2014-10-11 01:03:00"
2 | 123    | 1      |  "2014-10-11 00:48:00"
3 | 123    | 1      |  "2014-10-10 21:14:00"
4 | 123    | 1      |  "2014-10-10 23:47:00"
1 | 123    | 1      |  "2014-10-11 08:01:00"
1 | 123    | 1      |  "2014-10-11 08:02:00"
1 | 123    | 1      |  "2014-10-11 08:03:00"

我所做的是更新表bbb。

actual_timein,然后是actual_timeout的另一条语句

代码语言:javascript
复制
UPDATE aaa c
  INNER JOIN(
      select *, MIN(time_log) as my_time from aaa w
         inner join bbb b on b.emp_id = a.emp_id and status = 1
and time_log < shift_in and TIME_TO_SEC(TIMEDIFF(a.shift_in, b.time_log))/3600  < 2
where b.timelog between '2014-10-01' and  '2014-10-31'
group by a.emp_id order by b.timelog asc
    ) d on d.emp_id = c.emp_id 
set c.actual_timein = d.my_time

假设代码有效:) ..。

还有别的办法吗?

如果我有200名员工,表中每15天将有3000名员工,附表200*15。

谢谢..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-05 16:05:16

这是您的查询:

代码语言:javascript
复制
UPDATE aaa c INNER JOIN
       (select *, MIN(time_log) as my_time
        from aaa w inner join
             bbb b
             on b.emp_id = a.emp_id and b.status = 1 and time_log < shift_in and 
                TIME_TO_SEC(TIMEDIFF(a.shift_in, b.time_log))/3600  < 2
        where b.timelog between '2014-10-01' and '2014-10-31'
        group by a.emp_id
        order by b.timelog asc
      ) d
      on d.emp_id = c.emp_id 
    set c.actual_timein = d.my_time;

初始观察:*是不必要的(在聚合查询中也是不明智的)。order by是不必要的。所以,试试这个版本:

代码语言:javascript
复制
UPDATE aaa c INNER JOIN
       (select a.emp_id MIN(time_log) as my_time
        from aaa w inner join
             bbb b
             on b.emp_id = a.emp_id and b.status = 1 and b.time_log < shift_in and 
                TIME_TO_SEC(TIMEDIFF(a.shift_in, b.time_log))/3600  < 2
        where b.timelog between '2014-10-01' and '2014-10-31'
        group by a.emp_id
      ) d
      on d.emp_id = c.emp_id 
    set c.actual_timein = d.my_time;

其次,索引可能会有所帮助。对于内部查询,我建议使用bbb(status, time_log, emp_id)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27318511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档