Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Prosedur tersimpan untuk mendaftar semua minggu di antara dua tanggal

Anda dapat memilih semua hari dan memfilter pada hari pertama dalam seminggu (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

Anda dapat menggunakan

AND DAYOFWEEK(selected_date) = 1

bukannya

date_format(selected_date, '%w') = 0

Saya pikir ini sedikit lebih mudah dibaca:http://sqlfiddle.com/#!2 /7bc0e/54




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Kesalahan PHP:Panggil ke fungsi anggota rowCount() pada non-objek

  2. Hubungkan beberapa tabel di database yang berbeda? MySql

  3. Kapan kami memilih DateTime daripada Timestamp

  4. Bagaimana cara menyimpan teks arab di database mysql menggunakan python?

  5. Tutorial bergabung sendiri #10 di sqlzoo