sql >> Databasteknik >  >> RDS >> Mysql

MySQL:Välj alla datum inom ett intervall även om inga poster finns

Jag hoppas att du kommer att reda ut resten.

select  * from (
select date_add('2003-01-01 00:00:00.000', INTERVAL n5.num*10000+n4.num*1000+n3.num*100+n2.num*10+n1.num DAY ) as date from
(select 0 as num
   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) n1,
(select 0 as num
   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) n2,
(select 0 as num
   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) n3,
(select 0 as num
   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) n4,
(select 0 as num
   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) n5
) a
where date >'2011-01-02 00:00:00.000' and date < NOW()
order by date

Med

select n3.num*100+n2.num*10+n1.num as date

du får en kolumn med siffror från 0 till max(n3)*100+max(n2)*10+max(n1)

Eftersom vi här har max n3 som 3 kommer SELECT att returnera 399 plus 0 -> 400 poster (datum i kalendern).

Du kan ställa in din dynamiska kalender genom att begränsa den, till exempel från min(datum) du måste till nu().



  1. OdbcConnection returnerar kinesiska tecken som ?

  2. Hur krypterar man lösenordet i Oracle?

  3. Skadliga, genomgripande SQL Server-prestandamyter

  4. Pivotera en tabell i SQL (d.v.s. korstabulering/korstabulering)