mysql如何查询时间间隔大于5分钟的数据(时间从现在往前推)
以下为oracle的相关查询方法http://bbs.csdn.net/topics/390223691哪位大侠可以更改为mysql的。dtimename--------...
以下为oracle 的相关查询方法http://bbs.csdn.net/topics/390223691哪位大侠可以更改为mysql 的。
dtime name
----------------------- ----
2012-09-09 12:00:00.000 aaa
2012-09-09 12:03:00.000 aaa
2012-09-09 12:06:00.000 aaa
2012-09-09 12:09:00.000 aaa
2012-09-09 12:12:00.000 aaa
--------------------期待结果--------------------
dtime name
----------------------- ----
2012-09-09 12:00:00.000 aaa
2012-09-09 12:06:00.000 aaa
2012-09-09 12:12:00.000 aaa 展开
dtime name
----------------------- ----
2012-09-09 12:00:00.000 aaa
2012-09-09 12:03:00.000 aaa
2012-09-09 12:06:00.000 aaa
2012-09-09 12:09:00.000 aaa
2012-09-09 12:12:00.000 aaa
--------------------期待结果--------------------
dtime name
----------------------- ----
2012-09-09 12:00:00.000 aaa
2012-09-09 12:06:00.000 aaa
2012-09-09 12:12:00.000 aaa 展开
3个回答
展开全部
select dtime,name from
(
select name,dtime,rank,ptime,ptime2 from(
select cg_tmp.*, @rownum :=@rownum + 1,
if(TIMESTAMPDIFF(MINUTE, @ptime,cg_tmp.dtime)<5,@rank:=@rank ,@rank:=@rank+1) as rank,
@ptime2:=cg_tmp.dtime as ptime2,
if(TIMESTAMPDIFF(MINUTE, @ptime,cg_tmp.dtime)<5,@ptime=null,@ptime:=@ptime2) as ptime
from
(
select * from `timerecord` order by dtime
) cg_tmp,
(select @rownum :=0 , @ptime := null ,@rank:=0,@ptime2 := null) a
) result
) a
where ptime is not null
以前回答过类似的时间间隔问题。
更多追问追答
追问
很给力哦,虽然看不太懂,顺便问一下,我想在查询出的记录中,取末尾的N条记录,怎么改,需要升序排列。
追答
select * from (
select dtime,name from
(
select name,dtime,rank,ptime,ptime2 from(
select cg_tmp.*, @rownum :=@rownum + 1,
if(TIMESTAMPDIFF(MINUTE, @ptime,cg_tmp.dtime)<5,@rank:=@rank ,@rank:=@rank+1) as rank,
@ptime2:=cg_tmp.dtime as ptime2,
if(TIMESTAMPDIFF(MINUTE, @ptime,cg_tmp.dtime)<5,@ptime=null,@ptime:=@ptime2) as ptime
from
(
select * from `timerecord` order by dtime
) cg_tmp,
(select @rownum :=0 , @ptime := null ,@rank:=0,@ptime2 := null) a
) result
) a
where ptime is not null
order by dtime desc limit 2 --这里取末尾的N条记录
) a order by dtime
展开全部
这个得用存储过程了,一句话查询肯定解决不了。
delimiter //
Create Procedure findtime()
Begin
declare lastdtime datetime default null;
declare thisdtime datetime default null;
declare lastname varchar(10);
declare thisname varchar(10);
declare done tinyint default 0;
declare cur cursor for select dtime,name from `table`;
declare continue handler for sqlstate '02000' set done=1;
create temporary table if not exists `tmp`(dtime datetime, name varchar(10));
while done<>1 do
if lastdtime is null then
fetch cur into lastdtime,lastname;
else
fetch cur into thisdtime,thisname;
if timediff(thisdtime,lastdtime)>'00:05:00' then
insert into `tmp` (dtime,name)values(lastdtime,lastname);
set lastdtime=thisdtime;
set lastname=thisname;
end if;
end if;
end while;
select * from `tmp`;
End//
call findtime()//
delimiter //
Create Procedure findtime()
Begin
declare lastdtime datetime default null;
declare thisdtime datetime default null;
declare lastname varchar(10);
declare thisname varchar(10);
declare done tinyint default 0;
declare cur cursor for select dtime,name from `table`;
declare continue handler for sqlstate '02000' set done=1;
create temporary table if not exists `tmp`(dtime datetime, name varchar(10));
while done<>1 do
if lastdtime is null then
fetch cur into lastdtime,lastname;
else
fetch cur into thisdtime,thisname;
if timediff(thisdtime,lastdtime)>'00:05:00' then
insert into `tmp` (dtime,name)values(lastdtime,lastname);
set lastdtime=thisdtime;
set lastname=thisname;
end if;
end if;
end while;
select * from `tmp`;
End//
call findtime()//
追问
麻烦把内容详细解释一下好吗,学的不好,看的一知半解。拜托了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SELECT * from table where
TIMEDIFF('1997-12-31 23:59:59', '1997-12-30 01:01:01')>'00:05:00';
TIMEDIFF('1997-12-31 23:59:59', '1997-12-30 01:01:01')>'00:05:00';
追问
是上下记录时间比较
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询