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列(可能)?谢谢.
解决方法:
假设每天只有一个进/出,那就像在日期和更长的时间自我加入一样简单.
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 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