日期:2014-05-17  浏览次数:20651 次

请教SQL查询语句


字段分别是   sted   tm2   tm1   tm  
怎么样查出 当 tm>=10 时 tm2 与 tm1 之间缺少的时间间隔是5分钟
比如 当tm=10时 tm1与tm2之间就是差一个五分钟 现在怎么样查询才给显示这个少的5分钟


举例:62702670 2013-6-5 7:50:00 2013-6-5 8:00:00 10 

      想得到的结果是:63202670  2013-6-5 7:55:00

不知道能否实现,请高手帮忙!!!!

------解决方案--------------------
select t.sted, t.tm2 + 5 / (24 * 60)   
from t  where t.tm >= 10    and t.tm1 - t.tm2 <=5/(24*60);
------解决方案--------------------
用datediff来实现获取时间的间隔select tm=datediff(ms,tm2,tm1)单位是毫秒!
------解决方案--------------------
每个数据库对时间的运算方法都不一样
--ORACLE
select t.sted, t.tm2 + 5 / (24 * 60)
  from t
 where t.tm >= 10
   and (t.tm1 - t.tm2) * 24 * 60 = 10;

--SQL SERVER
select t.sted, DATEADD(mi,5,t.tm2)
  from t
 where t.tm >= 10
   and DATEDIFF(mi,t.tm2,t.tm1) = 10;