日期:2014-05-18  浏览次数:20524 次

求sql:把购物车内的记录超过8小时的删除
把购物车内的记录超过8小时的删除
表 Car
字段 carDate(加入购物车的时间)

不知道这样写是否正确:Delete From Car WHERE datediff(hh,carDate,getdate())>=8
datediff是否精确,最好不要8小时不到就删除了,这样的话商品无法加入购物车了,也不要超过8小时很久也没有删除,求正确的sql。

------解决方案--------------------
SQL code

declare @t table(col datetime)
insert into @t
select '2012-01-31 19:37:57.450' union all
select '2012-01-31 11:40:57.450' union all
select '2012-01-31 11:39:57.450' union all
select '2012-01-31 11:39:58.450' union all
select '2012-01-31 11:37:57.450' union all
select '2012-01-31 11:41:57.450' union all
select '2012-01-31 11:42:57.450' 
--得到8小时以前的数据
select * from @t where datediff(hh,col,getdate())>=8
/*
col
-----------------------
2012-01-31 11:40:57.450
2012-01-31 11:39:57.450
2012-01-31 11:39:58.450
2012-01-31 11:37:57.450
2012-01-31 11:41:57.450
2012-01-31 11:42:57.450
*/
--得到480分钟以前的数据
select * from @t where datediff(mi,col,getdate())>=480
/*
col
-----------------------
2012-01-31 11:40:57.450
2012-01-31 11:39:57.450
2012-01-31 11:39:58.450
2012-01-31 11:37:57.450
2012-01-31 11:41:57.450
*/

------解决方案--------------------
Delete From Car WHERE datediff(mi,carDate,getdate())>= (8 * 60)

------解决方案--------------------
delete from tbl where datediff(mi,carDate,getdate())>= 480