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

MySQL WEEK() :Dapatkan semua minggu dalam rentang tanggal (dengan/tanpa catatan)

Dengan asumsi Anda memiliki tabel bilangan bulat (disebut `numbers` bawah):

   SELECT COALESCE(n, 0) AS num_complaints, CONCAT('Week ', i) AS `week`
     FROM (SELECT i
             FROM numbers
            WHERE i BETWEEN (SELECT WEEK(MIN(complaintRaisedDate)) FROM events LIMIT 1)
                            AND
                            (SELECT WEEK(MAX(complaintRaisedDate)) FROM events LIMIT 1))
          week_ranges
LEFT JOIN (  SELECT count(id) AS n, WEEK(complaintRaisedDate) AS weeknum
               FROM events
              WHERE categoryId=1
           GROUP BY weeknum) weekly_tallies
       ON week_ranges.i = weekly_tallies.weeknum
 ORDER BY `week` ASC;

SQL biola



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. BLOB vs. VARCHAR untuk menyimpan array dalam tabel MySQL

  2. Membangun hubungan ternary menggunakan Laravel Eloquent Relationships

  3. Bagaimana cara membuat kueri lintas basis data di MySQL?

  4. Batas waktu kueri global di MySQL 5.6

  5. Permintaan SQL untuk menampilkan catatan