2016-03-03 122 views
0

我有一個日期列表,我希望它們在我的應用程序的日曆小工具中啓用。日曆小部件允許我只給出需要禁用的日期列表。選擇一年中的所有日期

enter image description here

我可以轉換成上述日期的格式2015-3-6,2015-3-8,2015-3-7,2015-5-4,2015-5-7,2015 -5-12,2015-6-16,2015-7-2,2015-6-19

但我需要在上述格式中獲得日曆中上述日期的其餘日期。我不知道我怎麼得到。任何想法,我怎麼能做到這一點是非常感謝

+0

你的意思是在SQL中創建日期列表? –

+0

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

+0

@JuanCarlosOropeza我沒有檢查發佈你提到的。但我不想在這裏使用存儲過程。有沒有使用存儲過程的方法。 – trx

回答

1

CREATE TABLE Available 
    (`IsAvailable` datetime); 

INSERT INTO Available 
    (`IsAvailable`) 
VALUES 
    ('2015-03-06 00:00:00'),  ('2015-03-08 00:00:00'), 
    ('2015-03-07 00:00:00'),  ('2015-05-04 00:00:00'), 
    ('2015-05-07 00:00:00'),  ('2015-05-12 00:00:00'), 
    ('2015-06-16 00:00:00'),  ('2015-07-02 00:00:00'), 
    ('2015-10-19 00:00:00'); 

SQL Fiddle Demo

select y2015.selected_date, 
     IF(Available.`IsAvailable` IS NULL, FALSE, TRUE) as Available 
from 
    (select adddate('2015-01-01', t2.i*100 + t1.i*10 + t0.i) selected_date 
     from 
     (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, 
     (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, 
     (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2 
    ) y2015 
LEFT JOIN Available 
     ON y2015.selected_date = Available.`IsAvailable` 
where y2015.selected_date < '2016-01-01' 

輸出

| selected_date | Available | 
|---------------|-----------| 
| 2015-03-06 |   1 | \ 
| 2015-03-08 |   1 | \ 
| 2015-03-07 |   1 | | 
| 2015-05-04 |   1 | \ 
| 2015-05-07 |   1 | /Those are in Avaliable table 
| 2015-05-12 |   1 | | 
| 2015-06-16 |   1 | | 
| 2015-07-02 |   1 | /
| 2015-10-19 |   1 | /
| 2015-01-01 |   0 | 
| 2015-01-02 |   0 | 
| 2015-01-03 |   0 | 
| 2015-01-04 |   0 | 
| 2015-01-05 |   0 | 
| 2015-01-06 |   0 | 
| 2015-01-07 |   0 | 
.... 
| 2015-12-31 |   0 | 

    Record Count: 365; 
+0

我感到困惑。我是否需要使用selected_date和IsAvailable字段創建名爲y2015的表。由於我的日期已插入到可用的SnapshotDate上,所以我不創建表格Available。請你詳細解釋律如何上述作品請 – trx

+0

y2015不是一個表,只是一個子查詢(讓我們稱之爲A)。如果你願意,你可以製作一張桌子。您也可以更改參數以獲得所需的日期範圍。我創建'可用'(讓我們叫它B),以便我可以測試我的查詢,你可以使用你的表。現在你只需要'(SELECT A. * FROM A LEFT JOIN B ON A.ID B.ID)'那麼你不知道哪部分? –

+0

因爲我在SnapShotDate表中有日期,所以我不想打擾他們。我創建了一個名爲AllDate的表,其中有兩個字段selected_date和Available。在2015年和2016年我將擁有所有日期,對於SnapshotDate表中的所有日期,可用值爲1。因此,如果我在AllDate Table中過濾可用爲0,那麼我應該在2015和2016年的剩餘日期。 – trx

相關問題