Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way to do it in one query:</p> <pre class="lang-sql prettyprint-override"><code>SELECT COUNT(my_table.id) AS total, CONCAT(YEAR(dates.ddate), '-', MONTH(dates.ddate), '-', DAY(dates.ddate)) FROM ( -- Creates "on the fly" 65536 days beginning from 2000-01-01 (179 years) SELECT DATE_ADD("2000-01-01", INTERVAL (b1.b + b2.b + b3.b + b4.b + b5.b + b6.b + b7.b + b8.b + b9.b + b10.b + b11.b + b12.b + b13.b + b14.b + b15.b + b16.b) DAY) AS ddate FROM (SELECT 0 AS b UNION SELECT 1) b1, (SELECT 0 AS b UNION SELECT 2) b2, (SELECT 0 AS b UNION SELECT 4) b3, (SELECT 0 AS b UNION SELECT 8) b4, (SELECT 0 AS b UNION SELECT 16) b5, (SELECT 0 AS b UNION SELECT 32) b6, (SELECT 0 AS b UNION SELECT 64) b7, (SELECT 0 AS b UNION SELECT 128) b8, (SELECT 0 AS b UNION SELECT 256) b9, (SELECT 0 AS b UNION SELECT 512) b10, (SELECT 0 AS b UNION SELECT 1024) b11, (SELECT 0 AS b UNION SELECT 2048) b12, (SELECT 0 AS b UNION SELECT 4096) b13, (SELECT 0 AS b UNION SELECT 8192) b14, (SELECT 0 AS b UNION SELECT 16384) b15, (SELECT 0 AS b UNION SELECT 32768) b16 ) dates LEFT JOIN my_table ON dates.ddate = my_table.created_at GROUP BY dates.ddate ORDER BY dates.ddate </code></pre> <p>The next code is only necessary if you want to test and don't have the "my_table" indicated on the question:</p> <pre class="lang-sql prettyprint-override"><code>create table `my_table` ( `id` int (11), `created_at` date ); insert into `my_table` (`id`, `created_at`) values('1','2000-01-01'); insert into `my_table` (`id`, `created_at`) values('2','2000-01-01'); insert into `my_table` (`id`, `created_at`) values('3','2000-01-01'); insert into `my_table` (`id`, `created_at`) values('4','2001-01-01'); insert into `my_table` (`id`, `created_at`) values('5','2100-06-06'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload