簡體   English   中英

如何從單個字段中查找超時和超時並將其值提取到SQL中的另一個表

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

我在PHP MyAdmin中有2個表,第一個是tb_data_log ,用於存儲數據記錄中的所有信息。 我需要從第一個表中找到time-intime-out ,然后將值提取到第二個表中,即tb_attendance 看下面的圖像:

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

我需要我的桌子看起來像下面這樣:

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

我認為這基本上很簡單,但是我不知道如何在MySQL中編寫代碼。 我應該怎么做? 謝謝!

此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`;

前提是您具有以下字段類型:

`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