我有这个问题,我有表XYZ这些值之间的次数差:MySQL的:最接近行
的product_id,USER_NAME,EVENT_TYPE,EVENT_TIME
ABCD123,用户1,REPAIR,2017年1月16日14点51分28秒
ABCD123,用户1,REPAIR DONE,2017年1月16日15时51分28秒
ABCD123,用户1,修理,2017年1月16日16时51分二十八秒
ABCD123,user1, REPAIR DONE,2017-01-16 18:51:28
怎样才能获得第一次维修完成和第一次维修和第二次维修完成和第二次维修之间的时间差? 我尝试这样做:
select a.product_id, a.user_name, b.event_time as start_time, a.event_time as end_time, FORMAT((TIMESTAMPDIFF(SECOND, b.event_time, a.event_time)/3600),2) as difference
FROM XYZ a
join XYZ b
ON a.product_id = b.product_id and a.user_name = b.user_name and a.event_type = 'REPAIR DONE' and b.event_type = 'REPAIR'
group by a.product_id, a.user_name
order by a.event_time asc
但是从某些原因,结果是这样的:
ABCD123,用户1,差异1小时
ABCD123,用户1,差4小时
正确的是:
ABCD123,用户1,差1小时
ABCD123,用户1,差2小时
换句话说,在第二行我得到第一修复和DONE DONE代替第二修复和第二修复第二修复之间的差的结果。
有人可以给我一些建议吗?
我很抱歉,我无法让您的解决方案正常工作,但可能是我的问题。 – Raymond
你可以在http://sqlfiddle.com/上发布一些表格,我会检查它 –
你好,我通过电子邮件向你发送了样本数据,sqlfiddle对我来说似乎对第一次看起来有点困惑。对不起。谢谢你的尝试。 – Raymond