MySQL查询显示下个月内的所有小时数

时间:2012-01-14 02:10:36

标签: mysql database function date

我如何整理一个查询以显示下周的所有小时数,因为我想将时间表与此预约目的进行比较。

感谢您的帮助!

编辑 -

预期结果会很好,在9到5之间

| client_date | client_time |
10/01/2010 09:00:00
10/01/2010 10:00:00
10/01/2010 11:00:00
10/01/2010 12:00:00
10/01/2010 13:00:00
10/01/2010 14:00:00
10/01/2010 15:00:00
10/01/2010 16:00:00
10/01/2010 17:00:00

2 个答案:

答案 0 :(得分:1)

您需要创建一个表来存储日期和时间值。

CREATE TABLE calendarhours (caldaytime DATETIME);    

然后,您需要创建一个存储过程来遍历这两个日期,并将时间表时间的日期时间值插入表中。

DELIMITER $$    

CREATE DEFINER=`root`@`localhost` PROCEDURE `timesheetdays`(startdate DATETIME, enddate DATETIME)    
BEGIN    
    DECLARE tempdate DATETIME;    


    DELETE FROM `calendarhours`;    

    -- set the temp date to 9am of the start date     
    SET tempdate = DATE_ADD(DATE(startdate), INTERVAL '0 9' DAY_HOUR);     

    -- while the temp date is less than or equal to the end date, insert the date    
    -- into the temp table    
    WHILE ( tempdate <= enddate ) DO    
        BEGIN    
            -- insert temp date into temp table    
            INSERT INTO `calendarhours` (caldaytime) VALUES (tempdate);    
            -- increment temp date by an hour    
            SET tempdate = DATE_ADD(tempdate, INTERVAL '0 1' DAY_HOUR);    

            -- if the temp date is greater than 5 PM (17:00) then increment to the next day    
            IF TIMEDIFF(tempdate, DATE_ADD(DATE(tempdate), INTERVAL '0 17' DAY_HOUR)) > 0 THEN    
                BEGIN    
                    -- increment to the next day    
                    SET tempdate = DATE_ADD(DATE(tempdate), INTERVAL '1 9' DAY_HOUR);     
                    -- for business purposes, if the day is a Saturday or a Sunday increment     
                    -- until we reach Monday    
                    WHILE ( DAYNAME(tempdate) = 'Saturday' OR DAYNAME(tempdate) = 'Sunday' ) DO    
                        BEGIN    
                            SET tempdate = DATE_ADD(DATE(tempdate), INTERVAL '1 9' DAY_HOUR);     
                        END;    
                    END WHILE;    
                END;    
            END IF;    
        END;    
    END WHILE;    

    -- return all the inserted date and times    
    SELECT * FROM calendarhours ORDER BY caldaytime;    

END    

此程序将循环执行两个日期,从每天上午9点开始,每天下午5点(17:00)结束。当时间到达18:00时,程序增加到第二天,并在上午9点再次开始。

如果您正在进行标准的工作周时间表,那么如果该日等于星期六或星期日,它将递增到星期一。

为了测试这个,我使用了以下语句:

CALL `timesheetdays`(NOW(), DATE_ADD(DATE(NOW()), INTERVAL '5 0' DAY_HOUR));    
SELECT * FROM `calendarhours`;    

这将测试从今天到今天5天的程序,并根据需要显示小时数。第一个语句将记录添加到表中,然后返回记录,第二个语句返回表中的记录。

答案 1 :(得分:1)

您可以在存储过程中使用临时表。

DELIMITER ;;
DROP PROCEDURE IF EXISTS ListHours ;;
CREATE PROCEDURE ListHours()
BEGIN
DECLARE curDT DATETIME;
DECLARE today DATETIME ;
DECLARE nextSaturday DATETIME;
DECLARE nextSunday DATETIME;
DECLARE iterDate DATETIME;
DECLARE iterDateTime DATETIME;
DECLARE iterBound DATETIME;
DECLARE resDate DATETIME;
DECLARE resTime DATETIME;
DECLARE delta INT;
DROP TABLE IF EXISTS tempNextWeek;
CREATE TEMPORARY TABLE IF NOT EXISTS tempNextWeek
(
    client_date VARCHAR(20),
    client_time VARCHAR(20)
);
DELETE FROM tempNextWeek;
SET curDT = NOW();
SET today = ADDTIME(SUBTIME(curDT , TIME(curDT)) , '9:0:0');
SET delta = 8 - DAYOFWEEK(today);  
SET  nextSunday = ADDDATE(today , INTERVAL delta DAY);
SET  nextSaturday = ADDTIME(nextSunday , '6 0:0:0');
-- select today , delta , nextSaturday , nextSunday ;
SET iterDate = nextSunday;
WHILE iterDate <= nextSaturday DO
    SET iterDateTime = iterDate;
    SET iterBound = ADDTIME(iterDateTime, '8:0:0');
    WHILE iterDateTIme <= iterBound DO
        INSERT tempNextWeek (client_date, client_time) VALUE ( DATE_FORMAT(iterDateTime, '%Y-%m-%d'), DATE_FORMAT(iterDateTime, '%H:%i:%s') );
        SET iterDateTime = ADDTIME(iterDateTime , '1:0:0');
    END WHILE;
    SET iterDate = ADDTIME(iterDate , '1 0:0:0');
END WHILE ;
SELECT * FROM tempNextWeek;
-- drop table if exists tempNextWeek;
END;;
DELIMITER ;

CALL ListHours();