追答:
create procedure proc_t()
begin
create table t_test1 (
rowid bigint auto_increment primary key,
user_id varchar(20),
datetime datetime
);
truncate TABLE t_test1;
insert into t_test1 (user_id,datetime ) select user_id,datetime from test1 order by user_id;
select
a.user_id '用户id',a.datetime '当前日期',
(select c.datetime from t_test1 c where c.user_id = a.user_id and c.rowid>a.rowid limit 1) '下行日期',
TIMESTAMPDIFF(HOUR,str_to_date(a.datetime,'%Y-%m-%d %T'),str_to_date((select b.datetime from t_test1 b where b.user_id = a.user_id and b.rowid>a.rowid limit 1),'%Y-%m-%d %T')) '时间差'
from t_test1 a;
drop table t_test1;
end
运行结果
用户id 当前日期 下行日期 时间差
0 2014-07-08 01:11:00 2014-07-08 07:11:20\x09 6
0 2014-07-08 07:11:20 2014-07-10 00:11:41\x09 41
0 2014-07-10 00:11:41
1 2014-07-08 03:11:00 2014-07-09 02:10:00 22
1 2014-07-09 02:10:00
计算相邻时间差,用的存储过程
时间差单位小时,如果要精确到秒的话自己完善吧
你这个要是有一个自增id的主键的话就不用存储过程,一句sql搞定