0
我有一個表格,當值更改時保存條目,但是現在我需要獲取兩個日期之間每天的值。循環兩個日期以獲取值
所以在數據庫中,我有以下幾點:
2015-10-17 25
2015-10-12 20
2015-10-01 5
我需要的是得到的值整月
2015-10-01 5
2015-10-02 5
....
2015-10-12 20
2015-10-13 20
我將如何實現這一目標?我有下面的代碼
http://www.sqlfiddle.com/#!2/ca447e/1/0
CREATE PROCEDURE sp_GetMonthVals(IN StartDate DATETIME, IN EndDate DATETIME)
BEGIN
DECLARE CurrentDate DATETIME;
SET @CurrentDate = StartDate;
label1: WHILE @CurrentDate < @EndDate DO
SELECT @CurrentDate, DVal FROM Table1 WHERE MDate <= @CurrentDate ORDER BY MDate DESC LIMIT 1;
SET @CurrentDate = DATE_ADD(CurrentDate, INTERVAL 1 DAY);
END WHILE label1;
END;
CREATE TABLE Table1
(`id` int, `MDate` varchar(10), `DVal` int)
;
INSERT INTO Table1
(`id`, `MDate`, `DVal`)
VALUES
(1, '2015-10-17', 25),
(2, '2015-10-12', 20),
(3, '2015-10-01', 5)
;
CALL sp_GetMonthVals('2015-10-01', '2015-10-31');