注册 登录
编程论坛 SQL Server论坛

关于SQL时间字段查询

cybin 发布于 2008-10-06 08:30, 1291 次点击
我有一个表aa:
          id.   project ,endtime(时间字段)
          。。。 。。。  2008-9-20
我现在想查询当前时间跟字段里面的时间相差3天所有记录?
我用select * from aa where (getdate()-endtime=3)为什么会没有记录显示?
还有我用select *  from aa where endtime betweent getdate() and getdate()-3 也是没有记录显示?
我的语法是不是哪里出错?
有没有其他的sql语句。
9 回复
#2
师妃暄2008-10-06 08:47
select * from aa where datediff(day,getdate(),endtime)=3
#3
cybin2008-10-06 09:30
回复 2# 师妃暄 的帖子
我刚才已经用这个语句试过了,显示了还是没有记录?
#4
师妃暄2008-10-06 09:31
那就不是语法错误.而是逻辑错误了
#5
cybin2008-10-06 09:34
什么叫逻辑错误?我听不懂,我是新手?
#6
cybin2008-10-06 10:03
还有一种情况:
select * from aa where getdate()-endtime=3 一个记录都不会显示。
可是用select * from aa where getdate()-endtme<=3就会显示记录?这是怎么回事?
有什么办法解决我上面的问题吗?谢谢大家?
#7
cybin2008-10-06 10:11
回复 2# 师妃暄 的帖子
我找出原因了。
语句为:
select * from aa where datediff(day,endtime,getdate())=3.
开始时间和结束时间颠倒了。
#8
师妃暄2008-10-06 12:45
这就是逻辑错误

恭喜你
#9
ghw111ghw2008-10-06 14:46
哈哈,师妃暄太有才了
#10
西风独自凉2008-10-06 16:09
1