我在从考勤机分割数据时遇到问题。从中导出后的数据源如下:
id name att 1 John 01/04/2015 7:59:00 1 John 01/04/2015 17:44:00 1 John 02/04/2015 7:50:00 1 John 02/04/2015 18:14
指纹时间的记录(进出)保存在一列中。我想将数据拆分为:
id name in out 1 John 01/04/2015 7:59:00 01/04/2015 17:44:00 1 John 02/04/2015 7:50:00 02/04/2015 18:14:00
如何将这些记录拆分为MySQL或PHP中的2列(可能)?谢谢。
答案 0 :(得分:3)
你可以试试这个。
SELECT a.userID,a.name,min(a.att)as `in`,max(a.att) as `out`
FROM
(
SELECT userID,name,str_to_date(att,'%m/%d/%Y%T') as att,str_to_date(att,'%m/%d/%Y') as attd
FROM attendance
) as a
GROUP BY a.userID,a.attd
答案 1 :(得分:2)
假设每天只有一次进/出,就像在日期和更长时间内自我加入一样简单。
select t1.id, t1.name, t1.att as `in`, t2.att as `out`
from table1 t1
inner join table1 t2
on date(t1.att) = date(t2.att) and t1.id = t2.id
and t2.att > t1.att
如果你想用这些数据创建一个全新的表,那么你可以摆脱导入,你只需要使用这个查询作为create table
的输入,如下所示:
create table new_table
as
select t1.id, t1.name, t1.att as `in`, t2.att as `out`
from table1 t1
inner join table1 t2
on date(t1.att) = date(t2.att) and t1.id = t2.id
and t2.att > t1.att