MySQL Timediff计算时间为晚上10点至早上6点

时间:2011-01-30 11:53:06

标签: mysql

SELECT 
        (time_to_sec(
            timediff('2011-01-26 23:51:20', 
                     '2011-01-26 15:51:10')
            ) /60 /60) 
    AS totalhours

总小时数为8.我该怎么办才能计算晚上10点到早上6点之间的小时数?

2 个答案:

答案 0 :(得分:2)

如果你使用它,那肯定更简单:

SELECT hour(timediff('2011-01-26 23:51:20', 
                     '2011-01-26 15:51:10'))
    AS totalhours

答案 1 :(得分:0)

使用timetosec,其他方法总能给你一个积极的结果......即

SELECT 
        (hour(
            timediff('2011-01-29 23:51:20', 
                     '2011-01-26 15:51:10')
            ) ) 
    AS totalhours

=80

SELECT 
        (hour(
            timediff('2011-01-26 23:51:20', 
                     '2011-01-29 15:51:10')
            ) ) 
    AS totalhours

= 63