小弟要实现一个功能,就是查询 当时间小于当前时间 或者 时间在今天到往后的10天 并且 用户名是john的信息,可是我这样写碰到了个问题,数据库只查询了小于当前时间并且用户名是john的信息,忽略了今天往后10天的信息,我试着在 (usertime<getdate() or usertime between getdate() and getdate()+10)加个括号,结果什么信息都没了,求解应该如何写呢? 
select  *  from  table  where  usertime<getdate() or usertime between getdate() and getdate()+10 and username='john'

解决方案 »

  1.   

    getdate()+10 
    日期不能这么加,使用函数DATEadd函数
      

  2.   

    DATEADD (datepart , number, date )
     
    参数
    datepart指定要返回新值的日期的组成部分。下表列出了 Microsoft SQL Server 2005 可识别的日期部分及其缩写。日期部分  缩写  
    year
     yy, yyyy
     
    quarter
     qq, q
     
    month
     mm, m
     
    dayofyear
     dy, y
     
    day
     dd, d
     
    week
     wk, ww
     
    weekday
     dw, w
     
    hour
     hh
     
    minute
     mi, n
     
    second
     ss, s
     
    millisecond
     ms
     
    number用于与 datepart 相加的值。如果指定了非整数值,则将舍弃该值的小数部分。例如,如果为 datepart 指定 day,为 number 指定 1.75,则 date 将增加 1。date表达式,用于返回 datetime 或 smalldatetime 值,或日期格式的字符串。有关指定日期的详细信息,请参阅 日期和时间 (Transact-SQL)。如果只指定年份的后两位数字,则小于或等于 two digit year cutoff 配置选项值的后两位数字的值将与截止年份处于同一世纪中。比此选项值的后两位数字大的值先于截止年份的世纪。例如,如果 two-digit year cutoff 为 2049(默认值),则 49 被解释为 2049,而 2050 被解释为 1950。为避免歧义,请使用四位的年份。 返回类型
    返回 datetime。但是,如果 date 参数为 smalldatetime,则返回 smalldatetime。
      

  3.   

    示例
    以下示例将输出 AdventureWorks 数据库中订单的时间范围的列表。此时间范围为当前订单日期加上 21 天。 复制代码 
    USE AdventureWorks;
    GO
    SELECT DATEADD(day, 21, OrderDate)AS TimeFrame 
    FROM Sales.SalesOrderHeader;
    GO
     
      

  4.   

    谢谢 我有个疑问就是  select * from table where usertime<getdate() or usertime between getdate() and getdate()+10这样的话是可以搜索正常的
      

  5.   

    呵呵,可能表里就没有今天往后10天关于john的记录
      

  6.   

    楼主,你给出的查询条件逻辑重组后,可以这样写
    where usertime < getdate() + 10 and username='john' 用这个条件试试。如果没有记录出来或者只有今天前的记录出来,那么说明你原来的代码没有问题,只是没有今天往后10天的数据。
      

  7.   

    我搜了,select * from table where usertime<getdate() or usertime between getdate() and getdate()+10这样的话是可以搜索正常的  就是说数据都有的
      

  8.   

    select * from table where usertime<getdate() or usertime between getdate() and getdate()+10 and username='john'改成:select * from table where (usertime<getdate() or usertime between getdate() and getdate()+10) and username='john'
    要加上括号
      

  9.   


    select * from table where usertime<getdate() or (usertime between getdate() and getdate()+10 and username='john')
      

  10.   

    这样只能搜索到usertime<getdate()的数据,其余就没了我测试过了select * from table where usertime<getdate() or usertime between getdate() and getdate()+10 是能搜索到全部的数据的 请高手帮忙解决
      

  11.   


    select * from table where usertime<getdate() 
    union all
    select * from table where usertime between getdate() and getdate()+10 and username='john'
      

  12.   

    --小于第10天就可以了
    select * from tb where usertime<dateadd(dd,10,getdate()) and username='john'
      

  13.   


    select * from table where usertime<getdate() and username='john'
    union 
    select * from table where usertime between getdate() and getdate()+10 and username='john'
      

  14.   

    没用的,只能搜索出 usertime<getdate()内容,如果把usertime between getdate() and getdate()+10放前面 就只能搜索出usertime between getdate() and getdate()+10的内容小弟用的是inner join链接  下面是源代码select t.t_id,v.LoginName,t.tName,t.model,t.chepai,t.carImg,t.sn,s.sim,s.simpwd,t.Consume,t.addtime,t.endtime from tb_snsell s
    inner join tb_terminal t on s.t_id=t.t_id inner join tb_vip v on t.vip_id=v.vip_id where
    t.endtime <getdate() or t.endtime between getdate() and getdate()+15 and v.LoginName='john'
      

  15.   

    inner join链接 了当然还可以union 只不过效率不高的