2014-11-17 119 views
3

有沒有辦法讓兩個日期之間的所有日期,而無需使用任何MySQL表選擇不使用一個表中的兩個日期之間的所有日期(生成日期列表)

喜歡的東西:

SELECT date BETWEEN '2012-02-10' AND '2012-02-15' 

那會導致這樣的:

out put date list 
2012-02-10 
... 
2012-02-15 
+0

在過程的循環將做到這一點的 –

+1

可能重複[獲取日期的兩個日期之間的列表(http://stackoverflow.com/questions/510012/get-a-list-of-dates -between-two-dates) – jpw

+0

檢查這個答案我最近給我保存日期在tabe,你可以使用選擇部分http://stackoverflow.com/questions/26944750/create-a-temporary-table-with-日期/ 26944861#26944861 –

回答

10

下面是該查詢:
這個查詢給出正確的結果在這兩個數據庫中:MariaDB & MySQL

SELECT ADDDATE('2012-02-10', INTERVAL @i:[email protected]+1 DAY) AS DAY 
FROM (
SELECT a.a 
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a 
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b 
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c 
) a 
JOIN (SELECT @i := -1) r1 
WHERE 
@i < DATEDIFF('2012-02-15', '2012-02-10') 
+0

hi @ kruti-patel,謝謝你的查詢。我很輕鬆地創建了一個存儲過程,我會請你解釋這個查詢背後的邏輯。先謝謝你。 – Matija

+0

hi @MatijaBaric,From子句將創建組合值,select子句將從startdate開始,並且每次將日期遞增1,直到兩個日期之間的差異未實現。 –

相關問題