开发者

MySQL Timediff Count hours between 10pm to 6am

开发者 https://www.devze.com 2023-02-07 06:43 出处:网络
SELECT 开发者_运维知识库(time_to_sec( timediff(\'2011-01-26 23:51:20\', \'2011-01-26 15:51:10\')
SELECT 
        开发者_运维知识库(time_to_sec(
            timediff('2011-01-26 23:51:20', 
                     '2011-01-26 15:51:10')
            ) /60 /60) 
    AS totalhours

The total hours is 8. What do I have to do so that it only count the hours between 10pm and 6am?


It's definitely simpler if you use:

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


Use timetosec, other method always gives you a positive result... ie

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
0

精彩评论

暂无评论...
验证码 换一张
取 消