SORU
28 Ocak 2010, PERŞEMBE


tarih, gün aralığı oluşturmak

Gibi bir sorgu çalıştırmak istiyorum

select ... as days where `date` is between '2010-01-20' and '2010-01-24'

Ve verileri gibi:

days
----------
2010-01-20
2010-01-21
2010-01-22
2010-01-23
2010-01-24

CEVAP
28 Ocak 2010, PERŞEMBE


Bu çözüm kullanırhiçbir döngüler, prosedürler, ya da geçici tablolar. Alt sorgu, son bin gün için bir tarih oluşturur, ve istediğiniz gibi geri veya ileri olarak gitmek için genişletilebilir.

select a.Date 
from (
    select curdate() - INTERVAL (a.a   (10 * b.a)   (100 * c.a)) DAY as Date
    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
where a.Date between '2010-01-20' and '2010-01-24' 

Çıkış:

Date
----------
2010-01-24
2010-01-23
2010-01-22
2010-01-21
2010-01-20

Performans ile ilgili notlar

here performansı şaşırtıcı derecede iyi olduğu ortaya test:yukarıdaki sorgu 0.0009 sn sürer.

Alt sorgu oluşturmak için yaklaşık uzatıyoruz. 100,000 numaraları (ve böylece tarih hakkında 274 yıl değer), 0.0458 saniye içinde çalışır.

Bu arada, bu küçük ayarlamalar ile en veritabanları ile çalışan çok taşınabilir bir tekniktir.

SQL Fiddle example returning 1,000 days

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • eurotuner

    eurotuner

    6 Mart 2006
  • Garrett Müller

    Garrett Mül

    26 HAZİRAN 2009
  • Rugiagialia

    Rugiagialia

    1 Ocak 2008