mysql如果订单日期中不存在数据

时间:2013-08-13 10:43:33

标签: mysql

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?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;