表有三个字段
id varchar(20)  primary key, 
USE_TIME datetime    #订购时间
UNUSE_TIME datetime   #退订时间 要求分别查询到2011年7月11日 24点为止 订购后当天内退订、3天内退订、10天内退订、一个月内退订、3个月内退订的用户有多少  
感觉要先把USE_TIME <= 2011-07-11 24:00:00的数据统计出来 然后分别算UNUSE_TIME减去USE_TIME为0天、3天、10天、1个月、3个月的id数量  具体的sql语句要如何写 
谢谢大家了

解决方案 »

  1.   

    使用 todays() 函数吧,可以转换为天数进行对比,很方便。
      

  2.   

    select case 
    when UNUSE_TIME<=USE_TIME + interval 1 day then '当天内退订'  
    when UNUSE_TIME<=USE_TIME + interval 3 day then '3天内退订'  
    when UNUSE_TIME<=USE_TIME + interval 10 day then '10天内退订'  
    when UNUSE_TIME<=USE_TIME + interval 1 month then '一个月内退订'  
    when UNUSE_TIME<=USE_TIME + interval 3 month then '3个月内退订'  
    end,
    count(*)
    from 表有三个字段
    where USE_TIME<'2011-07-11 24:00:00'
    group by case 
    when UNUSE_TIME<=USE_TIME + interval 1 day then '当天内退订'  
    when UNUSE_TIME<=USE_TIME + interval 3 day then '3天内退订'  
    when UNUSE_TIME<=USE_TIME + interval 10 day then '10天内退订'  
    when UNUSE_TIME<=USE_TIME + interval 1 month then '一个月内退订'  
    when UNUSE_TIME<=USE_TIME + interval 3 month then '3个月内退订'  
    end