mysql如何查询时间间隔大于5分钟的数据(时间从现在往前推)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql如何查询时间间隔大于5分钟的数据(时间从现在往前推)相关的知识,希望对你有一定的参考价值。
以下为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
(
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追问
索性脸皮厚点再问个问题了,
如何显示一个percent字段用来计算name字段在当前ID号(升序)时出现的次数百分比(所以要在计算间隔之前就做一下计算?)
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()//追问
麻烦把内容详细解释一下好吗,学的不好,看的一知半解。拜托了。
参考技术B SELECT * from table whereTIMEDIFF('1997-12-31 23:59:59', '1997-12-30 01:01:01')>'00:05:00';追问
是上下记录时间比较
如何计算从上午 8:00 到晚上 8:00 以 15 分钟为间隔的可用时间?
【中文标题】如何计算从上午 8:00 到晚上 8:00 以 15 分钟为间隔的可用时间?【英文标题】:How to calculate available times in a 15 Minutes Interval from 8:00 am to 8:00 pm? 【发布时间】:2014-01-02 13:42:54 【问题描述】:我想开发一个 iOS 应用程序,它可以在上午 8:00 到晚上 8:00 的 15 分钟间隔内为您提供预约时间,条件是您的预约时间为 1 小时 30 分钟。
App 将下载这一次并将它们粘贴到 tableView 中。其他约会的时间(开始、结束时间、持续时间)将存储在 MySQL 数据库中。我想用PHP或者SQL来计算(不知道哪个更好)。
到目前为止,这是我的想法:
function easyfunction($day, $cutter, $open, $closed)
//Create new Array
$frei = array();
//Calculate times for the $frei-Array
for($time = $open; $time > $closed; $time=$time + date_create_from_format('H:i', 0:15);)
array_push($frei, $time);
//MySQL-Request
$connect = mysqli_connect("host", "DB", "Password")or die("Fehler beim Verbinden mit der Datenbank");
mysqli_select_db("Appointments")or die("Database doesnt exist");
$sql = "SELECT * FROM termine WHERE friseuse=$cutter AND date=$day";
$ergebnis = mysqli_query($sql);
while($row = mysqli_fetch_array($ergebnis))
//Write Appointment and duration in variables
$datetime = $row->datetime;
$duration = $row->duration;
//Calculate Ending
$terminende = $datetime + $duration;
// Create Search Array
$search = array();
//Filter all values from $frei
$search = array_search($datumzeit < $frei, $frei);
$search = array_search($ende > $frei , $frei);
unset($frei[$search]);
//Return all times
return $frei;
好吧,这段代码不包含顶部的给定条件,但我想添加它,如果我可以构建一个工作代码。
【问题讨论】:
我希望那不是您的真实用户名和密码。 Find first free date in agenda 的可能重复项 谢谢开发者空居民!我会在几天内尝试一下! :-) 嗯,不完全是这样。我想要只有一天的时间。我必须替换 NOW() 吗?对不起,我只知道 SQL 的基础知识。 【参考方案1】:在 SQL 中,我计算了时间间隔(我已根据您的要求进行了修改)
declare @time datetime
declare @Etime datetime
declare @Interval datetime
set @time='2013-12-18 08:00:00.000'
While @time<'2013-12-18 16:00:00.000'
Begin
set @Etime=convert(varchar(25), dateadd(mi,90,@time))
set @Interval = convert(varchar(25), dateadd(mi,15,@Etime))
select @time as StartTime, @Etime as EndTime, @Interval as Interval
set @Time = @Interval
End
在我的项目中,我将其用作
declare @time datetime
declare @Etime datetime
declare @Interval datetime
set @time='2013-12-18 08:00:00.000'
IF OBJECT_ID('dbo.timetable', 'U') IS NOT NULL
DROP TABLE dbo.timetable
create table timetable (StartTime datetime, EndTime datetime, Interval datetime)
While @time<'2013-12-18 16:00:00.000'
Begin
set @Etime=convert(varchar(25), dateadd(mi,90,@time))
set @Interval = convert(varchar(25), dateadd(mi,15,@Etime))
insert into timetable values(@time, @Etime, @Interval)
set @Time = @Interval
End
以后
select * from timetable
希望对你有帮助
【讨论】:
以上是关于mysql如何查询时间间隔大于5分钟的数据(时间从现在往前推)的主要内容,如果未能解决你的问题,请参考以下文章
sql 分组查询time时间间隔大于30分钟的两行,列出2条记录,并列出每组首尾记录,求大神解决!