数据库结构:
         id       hit       times
1 1 2008年2月19日
2 26 2008年2月27日
3 32 2008年2月27日select sum(hit)as wk,times from hit where id=1005 group by times
得到结果:
1 2009年12月10日
2 2009年12月16日
1 2009年12月17日
1 2009年12月19日
983 2009年12月1日
1262 2009年12月2日
1201 2009年12月3日
857 2009年12月4日
951 2009年12月5日
924 2009年12月6日
312 2009年12月7日
4 2009年12月8日
2 2009年12月9日
select sum(hit)as wk,times from hit where id=1005 and replace(replace(replace(times,'年','-'),'月','-'),'日','')>=replace(replace(replace('2009年12月20日','年','-'),'月','-'),'日','') group by times
得到结果:
1201 2009年12月3日
857 2009年12月4日
951 2009年12月5日
924 2009年12月6日
312 2009年12月7日
4 2009年12月8日
2 2009年12月9日奇怪为何最后的语句会得到这样的结果?求高人指点。。

解决方案 »

  1.   

    你REPLACE完了是不是应该用CONVERT转一下再比较
      

  2.   

    select sum(hit)as wk,times from hit where id=1005 and CONVERT(DATETIME,replace(replace(replace(times,'年','-'),'月','-'),'日 ',''))>=CONVERT(DATETIME,replace(replace(replace('2009年12月20日','年','-'),'月','-'),'日 ','')) group by times
      

  3.   

    select sum(hit)as wk , cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') as datetime) 
    from hit where id=1005 and cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') <= '2009-12-20' 
    group by cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','')
      

  4.   

    你的times 字段不能用datetime 吗
    或者格式化一下就不会有问题2009年12月3日
    格式化成
    2009年12月03日2009年9月3日
    格式化成
    2009年09月03日
      

  5.   

    select sum(hit)as wk,times from hit where id=1005 
    and cast(replace(replace(replace(times,'年','-'),'月','-'),'日','') as datetime)
    >=cast(replace(replace(replace('2009年12月20日','年','-'),'月','-'),'日','') as datetime) 
      

  6.   

    select sum(hit)as wk , cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') as datetime) 
    from hit 
    where id=1005 and cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') as datetime) <= '2009-12-20' 
    group by cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') as datetime)
      

  7.   

    select
     sum(hit)as wk , 
     cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') as datetime) 
    from
     hit where id=1005 
    and
     cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') <= '2009-12-20' 
    group by
     cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','')
      

  8.   

    如果格式化了倒可以直接select sum(hit)as wk,times from hit where id=1005 and times>='2009年12月20日'
    group by times 
      

  9.   

    你转换成2009-12-20 后再cast( ..  as datetime) 比较就不会有问题了
      

  10.   

    日期这样转成字符串比较会有问题,字符串比较是一个一个字符依次比较的,所以还是转成datetime后再比较
      

  11.   

    还有就是为什么数据库中字段类型不直接用datetime?
      

  12.   


    之前用ACCESS的,在ACCESS下直接比较就可以的,好像在ACCESS是可以自己转换的。做这是很小的东西,主要是自己用的,效率不用考虑,只是写的时候感觉用什么类型方便就用什么了。
      

  13.   


    加个GROUP BY 就OK了,谢谢,给分