我有一个具有“约会表”和“服务表”的数据库。每个appt都有一个服务,每个服务都有一个价格。我想要的是一个查询,它将始终返回 12 行(每个月一行)并包含月份 appts 的总和(基于它的服务 ID)。到目前为止,我有:
select sum(service_price) as monthly_total,
year(appt_date_time) as year,
monthname(appt_date_time) as month
from appt_tbl
join services_tbl on appt_tbl.service_id = services_tbl.service_id
group by month(appt_date_time),
year(appt_date_time)
order by month(appt_date_time) asc;
目前,这会返回如下内容:
+---------------+------+-------+
| monthly_total | year | month |
+---------------+------+-------+
| 120.00 | 2012 | July |
+---------------+------+-------+
问题是如果一个月没有任何应用程序,我不会在查询中返回该月。我希望那个月有一个记录,只要它的“monthly_total”等于零。
以下是我希望查询返回的内容:
+---------------+------+-------+
| monthly_total | year | month |
+---------------+------+-------+
| 0.00 | 2012 | Jan |
+---------------+------+-------+
| 0.00 | 2012 | Feb |
+---------------+------+-------+
| 0.00 | 2012 | March |
+---------------+------+-------+
| 0.00 | 2012 | April |
+---------------+------+-------+
| 0.00 | 2012 | May |
+---------------+------+-------+
| 0.00 | 2012 | June |
+---------------+------+-------+
| 120.00 | 2012 | July |
+---------------+------+-------+
| 0.00 | 2012 | August|
+---------------+------+-------+
| 0.00 | 2012 | Sept |
+---------------+------+-------+
| 0.00 | 2012 | Oct |
+---------------+------+-------+
| 0.00 | 2012 | Nov |
+---------------+------+-------+
| 0.00 | 2012 | Dec |
+---------------+------+-------+
有任何想法吗?