0

This is probably really easy, but I am having trouble with it. I asked a question earlier (MySql Query help) about a query and the answer was to create another table and join it with my current query. So I am creating a temporary table and joining it with my query but am getting the same results.

Overview: I have a dates tables with the dates for the last 30 days and a value of 0 for every entry

+------+------------+
| Zero | Date       |
+------+------------+
|    0 | 2011-07-27 |
|    0 | 2011-07-26 |
|    0 | 2011-07-25 |
|    0 | 2011-07-24 |
|    0 | 2011-07-23 |
|    0 | 2011-07-22 |
...etc

And I am trying to join this table with a query I do another table that gets these results:

+-------------------+-----------+
| Past-Month-Builds | Month-Day |
+-------------------+-----------+
|                53 | 6-27      |
|               103 | 6-28      |
|                91 | 6-29      |
|                70 | 6-30      |
|                76 | 7-1       |
|                 8 | 7-2       |
|                77 | 7-5       |
|               111 | 7-6       |
|                67 | 7-7       |
|                70 | 7-8       |
|                 2 | 7-9       |
|                 3 | 7-10      |
|                87 | 7-11      |
|                53 | 7-12      |
|                49 | 7-13      |
|                84 | 7-14      |
|               126 | 7-15      |
|                 3 | 7-16      |
|                 8 | 7-17      |
|                98 | 7-18      |
|               114 | 7-19      |
|               841 | 7-20      |
|               206 | 7-21      |
|               738 | 7-22      |
|                 2 | 7-23      |
|                65 | 7-25      |
|                39 | 7-26      |
|                21 | 7-27      |
+-------------------+-----------+

Notice that there are missing days, I would like the Past-Month-Builds Col to display 0 when there weren't any builds on that day.

Here is the query I am trying to use:

SELECT
    COUNT(id) AS 'Past-Month-Builds',
    CONCAT(MONTH(builds.submittime), '-', DAY(builds.submittime)) as 'Month-Day'
FROM builds
    RIGHT JOIN dates ON DATE(builds.submittime) = dates.Date
WHERE DATE(builds.submittime) >= DATE_SUB(CURDATE(), INTERVAL 30 day)
GROUP BY MONTH(submittime), DAY(submittime);

Kinda lost, any help would be great Thanks.

Community
  • 1
  • 1
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170

1 Answers1

1

Change the query so dates is the primary table and use a left outer join to the builds table.

Also, move any criteria related to the builds table from the WHERE clause to the ON clause since that's the optional table.

Ike Walker
  • 64,401
  • 14
  • 110
  • 109