0
SELECT
EXTRACT( DAY FROM date) AS day,
EXTRACT( MONTH FROM date) AS month, who, sum ( wsd ) AS total FROM weekly
WHERE season = '08'
AND status = 1
AND who = 'NAME SURNAME'
GROUP BY day, month


day month who           total
12  8     NAME SURNAME  18

我每天都会为“谁”列中的每个人获取“wsd”列的总数。如果一个人一天没有记录在表中,我们无法按预期在结果中看到该名称。

但我也想查看这些记录,日期和名称在总列中为“0”。

我怎么能只用mysql呢?

4

2 回答 2

0

您是否尝试使用 Coalesce 或 Isnull 插入默认值(在您的情况下为 0)?

于 2013-08-13T10:59:33.080 回答
0

对于这种情况,您可以将条件逻辑从聚合移动where到聚合——假设您每天都有某人的数据:

SELECT EXTRACT( DAY FROM date) AS day, EXTRACT( MONTH FROM date) AS month, who,
       sum(case when status = 1 AND who = 'NAME SURNAME' then wsd else 0 end) AS total
FROM weekly
WHERE season = '08'
GROUP BY day, month;
于 2013-08-13T11:03:38.603 回答