ID 产品 仿问数 日期
1 a 1 2006-8-1 1:23:2
2 b 3 2006-8-1 1:24:2
3 c 2 2006-8-1 1:25:2
4 a 5 2006-8-2 1:26:2
5 b 1 2006-8-2 1:33:2
6 c 4 2006-8-2 1:43:2
查询 最近15分钟的记录.该怎么写
1 a 1 2006-8-1 1:23:2
2 b 3 2006-8-1 1:24:2
3 c 2 2006-8-1 1:25:2
4 a 5 2006-8-2 1:26:2
5 b 1 2006-8-2 1:33:2
6 c 4 2006-8-2 1:43:2
查询 最近15分钟的记录.该怎么写
select * from tb where dateiff(n,日期,getdate())<=15
和表头记录差15分钟
select * from tb where dateiff(n,日期,(select top 1 日期 from tb))<=15
datediff(mi,A.日期,getdate())<=15
我的理解最近:
1.与当前时间相对
2.与最大时间相比.
declare @dt datetime
select @dt = max(日期) from Aselect * from A where
datediff(mi,A.日期,@dt)<=15
--> 测试数据: #t
if object_id('tempdb.dbo.#t') is not null drop table #t
create table #t ([ID] int,[产品] varchar(1),[仿问数] int,[日期] datetime)
insert into #t
select 1,'a',1,'2008-6-15 14:23:2' union all
select 2,'b',3,'2006-8-1 1:24:2' union all
select 3,'c',2,'2006-8-1 14:22:2' union all
select 4,'a',5,'2006-8-2 1:26:2' union all
select 5,'b',1,'2006-8-2 1:33:2' union all
select 6,'c',4,'2006-8-2 1:43:2'select * from #t where 日期 between dateadd(mi,-15,getdate()) and dateadd(mi,15,getdate())
/*ID 产品 仿问数 日期
----------- ---- ----------- -----------------------
1 a 1 2008-06-15 14:23:02.000*/
n代表参数:年,月,日..
然后是日期是和当前时间的差值
FROM 表
WHERE 时间 >= DATEADD(minute,-15,GETDATE())