English 中文(简体)
MySQL询问下个月所有小时的情况。
原标题:MySQL query to show all the hours within the next month

我将如何把下周所有时间都显示出来,因为我想为任用目的对时间表进行比较。

感谢任何帮助!

编辑

预期结果将在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

最佳回答

您可在储存程序中使用临时表格。

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();
问题回答

你们需要建立一个表格,储存日期和时间价值。

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)结束。 当时间到下午8时,程序将持续到第二天,9时再次开始。

If you are doing a standard business week timesheet, then if the day is equal to Saturday or Sunday, it will increment until it reaches Monday.

为测试我使用了以下陈述:

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

This tests the procedure from today to 5 days from today and shows the hours as required. The first statement adds the records to the table and then returns the records, the second statement returns the records from the table.





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签