您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

趋同一列不同条件形成的两行的时间差的sql语句

2024/3/14 5:03:22发布22次查看
求同一列不同条件形成的两行的时间差的sql语句
表 
create table `hp_report` (
`id` int(10) unsigned not null auto_increment,
`code` varchar(255) not null,
`content` mediumtext not null,
`ctime` datetime not null,
) engine=myisam default charset=utf8 auto_increment=662555 ;
insert into `hp_report` values (2, 'a', 'on', '2014-07-04 21:17:53');
insert into `hp_report` values (3, 'a', 'abc', '2014-07-04 21:18:53');
insert into `hp_report` values (4, 'a', 'off', '2014-07-04 21:19:53');
insert into `hp_report` values (5, 'b', 'on', '2014-07-04 21:20:53');
insert into `hp_report` values (6, 'b', 'abc', '2014-07-04 21:22:53');
insert into `hp_report` values (7, 'b', 'off', '2014-07-04 21:29:53');
insert into `hp_report` values (8, 'a', 'on', '2014-07-04 21:34:53');
insert into `hp_report` values (9, 'a', 'abc', '2014-07-04 21:36:53');
insert into `hp_report` values (10, 'a', 'off', '2014-07-04 21:45:53');
insert into `hp_report` values (11, 'b', 'on', '2014-07-04 22:12:53');
insert into `hp_report` values (13, 'b', 'abc', '2014-07-04 22:18:53');
insert into `hp_report` values (14, 'b', 'off', '2014-07-04 22:19:53');

我想求出a和b,content 为off,和on 之间的ctime的差值,并把差值求和
即:
insert into `hp_report` values (4, 'a', 'off', '2014-07-04 21:19:53');

insert into `hp_report` values (2, 'a', 'on', '2014-07-04 21:17:53');
之间ctime的差值(2分钟)
insert into `hp_report` values (8, 'a', 'on', '2014-07-04 21:34:53');
insert into `hp_report` values (10, 'a', 'off', '2014-07-04 21:45:53');
这个是11分钟。
并把a只有所有的差值加起来。
我要的结果是
a 13
b 16

求大神们帮帮忙。谢谢了!!
------解决方案--------------------
可以把时间取出来以后再计算吗?
strtotime($row['ctime']);
echo (strtotime('2014-07-04 21:19:53')-strtotime('2014-07-04 21:17:53')); 
结果是 120 单位秒
------解决方案--------------------
mysql> select * from hp_report;
+----+------+---------+---------------------+
------解决方案--------------------
 id 
------解决方案--------------------
 code 
------解决方案--------------------
 content 
------解决方案--------------------
 ctime               
------解决方案--------------------
+----+------+---------+---------------------+
------解决方案--------------------
  2 
------解决方案--------------------
 a    
------解决方案--------------------
 on      
------解决方案--------------------
 2014-07-04 21:17:53 
------解决方案--------------------
------解决方案--------------------
  3 
------解决方案--------------------
 a    
------解决方案--------------------
 abc     
------解决方案--------------------
 2014-07-04 21:18:53 
------解决方案--------------------
------解决方案--------------------
  4 
------解决方案--------------------
 a    
------解决方案--------------------
 off     
------解决方案--------------------
 2014-07-04 21:19:53 
------解决方案--------------------
------解决方案--------------------
  5 
------解决方案--------------------
 b    
------解决方案--------------------
 on      
------解决方案--------------------
 2014-07-04 21:20:53 
------解决方案--------------------
------解决方案--------------------
  6 
------解决方案--------------------
 b    
------解决方案--------------------
 abc     
------解决方案--------------------
 2014-07-04 21:22:53 
------解决方案--------------------

该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product