gpt4 book ai didi

php - 在 MysQL 或 PHP 中将记录从一列拆分为两列

转载 作者:可可西里 更新时间:2023-11-01 07:58:36 24 4
gpt4 key购买 nike

我在从考勤机拆分数据时遇到问题。导出后的数据源如下:

id name  att1  John  01/04/2015   7:59:001  John  01/04/2015  17:44:001  John  02/04/2015   7:50:001  John  02/04/2015  18:14

指纹时间的记录(进出)保存在一列中。我想将数据拆分成这样:

id name  in                  out1  John  01/04/2015 7:59:00  01/04/2015 17:44:001  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

sql fiddle demo

如果你想用这些数据创建一个全新的表,这样你就可以摆脱导入,你只需要使用这个查询作为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

关于php - 在 MysQL 或 PHP 中将记录从一列拆分为两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30251510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com