这是我写的SQL:
 select login_time,
        logout_time,
        pid,
        case 
         when time(login_time) >= '00:00:00' and time(logout_time) < '01:00:00' then '0~1 hours'
         when time(login_time) >= '01:00:00' and time(logout_time) < '02:00:00' then '1~2 hours'
         when time(login_time) >= '02:00:00' and time(logout_time) < '03:00:00' then '2~3 hours'
         when time(login_time) >= '03:00:00' and time(logout_time) < '04:00:00' then '3~4 hours'
         when time(login_time) >= '04:00:00' and time(logout_time) < '05:00:00' then '4~5 hours'
         when time(login_time) >= '05:00:00' and time(logout_time) < '06:00:00' then '5~6 hours'
         when time(login_time) >= '06:00:00' and time(logout_time) < '07:00:00' then '6~7 hours'
         when time(login_time) >= '07:00:00' and time(logout_time) < '08:00:00' then '7~8 hours'
         when time(login_time) >= '08:00:00' and time(logout_time) < '09:00:00' then '8~9 hours'
         when time(login_time) >= '09:00:00' and time(logout_time) < '10:00:00' then '9~10 hours'
         when time(login_time) >= '10:00:00' and time(logout_time) < '11:00:00' then '10~11 hours'
         when time(login_time) >= '11:00:00' and time(logout_time) < '12:00:00' then '11~12 hours'
         when time(login_time) >= '12:00:00' and time(logout_time) < '13:00:00' then '12~13 hours'
         when time(login_time) >= '13:00:00' and time(logout_time) < '14:00:00' then '13~14 hours'
         when time(login_time) >= '14:00:00' and time(logout_time) < '15:00:00' then '14~15 hours'
         when time(login_time) >= '15:00:00' and time(logout_time) < '16:00:00' then '15~16 hours'
         when time(login_time) >= '16:00:00' and time(logout_time) < '17:00:00' then '16~17 hours'
         when time(login_time) >= '17:00:00' and time(logout_time) < '18:00:00' then '17~18 hours'
         when time(login_time) >= '18:00:00' and time(logout_time) < '19:00:00' then '18~19 hours'
         when time(login_time) >= '19:00:00' and time(logout_time) < '20:00:00' then '19~20 hours'
         when time(login_time) >= '20:00:00' and time(logout_time) < '21:00:00' then '20~21 hours'
         when time(login_time) >= '21:00:00' and time(logout_time) < '22:00:00' then '21~22 hours'
         when time(login_time) >= '22:00:00' and time(logout_time) < '23:00:00' then '22~23 hours'
         when time(login_time) >= '23:00:00' and time(logout_time) < '00:00:00' then '23~24 hours'
        end as during_time 
 from
     。
但是提取出来的值不是很正确,如果跨度在2小时或者以上,以及从今天到明天,数值就不正确了,请教一下这样的SQL应该怎么写。谢谢。

解决方案 »

  1.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  2.   

    select login_time,  logout_time,  pid, logout_time - login_time as online_time from ..
    然后程序可以根据login_time和online_time来做进一步计算
      

  3.   

    hour(timediff(logout_time,login_time))