简体   繁体   English

如何从单个字段中查找超时和超时并将其值提取到SQL中的另一个表

[英]How to find time-in and time-out from a single field and extract its value to another table in SQL

I have 2 tables in PHP MyAdmin, first is tb_data_log that stores all information from data records. 我在PHP MyAdmin中有2个表,第一个是tb_data_log ,用于存储数据记录中的所有信息。 I need to find time-in and time-out from first table and then extract the value to second table that is tb_attendance . 我需要从第一个表中找到time-intime-out ,然后将值提取到第二个表中,即tb_attendance Look at image bellow: 看下面的图像:

tb_data_log tb_data_log

uid date time 1 28/01/2017 07.12 1 28/01/2017 16.02 2 28/01/2017 07.05 2 28/01/2017 16.23 3 28/01/2017 07.00 3 28/01/2017 16.16 1 29/01/2017 07.24 1 29/01/2017 16.11 2 29/01/2017 07.09 2 29/01/2017 16.45 3 29/01/2017 07.12 3 29/01/2017 16.02 1 30/01/2017 07.12 1 30/01/2017 16.02 2 30/01/2017 07.29 2 30/01/2017 16.19 3 30/01/2017 07.22 3 30/01/2017 16.56

I need my table to look like this bellow: 我需要我的桌子看起来像下面这样:

tb_attendance tb_attendance

uid date time_in time_out 1 28/01/2017 07.12 16.02 2 28/01/2017 07.05 16.23 3 28/01/2017 07.00 16.16 1 29/01/2017 07.24 16.11 2 29/01/2017 07.09 16.45 3 29/01/2017 07.12 16.02 1 30/01/2017 07.12 16.02 2 30/01/2017 07.29 16.19 3 30/01/2017 07.22 16.56

I think it is basically simple, but I have no idea how to write the codes in MySQL. 我认为这基本上很简单,但是我不知道如何在MySQL中编写代码。 How am I supposed to do that? 我应该怎么做? Thanks! 谢谢!

This SQL should do the task: 此SQL应该执行以下任务:

INSERT INTO `tb_attendance` 
SELECT `uid`, 
       `date`, 
       Min(`time`) AS 'time_in', 
       Max(`time`) AS 'time_out' 
FROM   `tb_data_log` 
GROUP  BY `date`, 
          `uid`;

Provided that you have the following field types: 前提是您具有以下字段类型:

`uid`  INT, 
`date` DATE, 
`time` TIME 
insert into tb_attendance 
    select a.uid,
          a.date,
          a.time as 'time_in',
          (select b.time from tb_data_log b where a.uid = b.uid and b.time >      
          a.time and a.date = b.date limit 1) as 'time_out' 
    from tb_data_log a group by a.uid,a.date order by a.date,a.uid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM