sql >> Database teknologi >  >> RDS >> Mysql

Gemt procedure for at liste alle uger mellem to datoer

Du kan vælge alle dage og filtrere på den første dag i ugen (date_format(selected_date, '%w') = 0 ):

select selected_date, date_add(selected_date, INTERVAL 6 DAY)
from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + 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,
(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) t3,
(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) t4) v
where selected_date between '2015-02-01' and '2015-02-28'
  AND date_format(selected_date, '%w') = 0

SQLFIDDLE:http://sqlfiddle.com/#!2/7bc0e/52

Du kan bruge

AND DAYOFWEEK(selected_date) = 1

i stedet for

date_format(selected_date, '%w') = 0

Jeg synes, det er lidt mere læseligt:​​http://sqlfiddle.com/#!2 /7bc0e/54




  1. Oracle opdelt bord

  2. henviser til flere fremmednøgler php mysql

  3. skriv resultater af sql-forespørgsel til en fil i mysql

  4. Forespørgsel ikke fuldført - MYSQL