如何为不存在的行返回0

时间:2017-05-04 10:08:21

标签: mysql

我有以下查询

SELECT `Product`,`Coverlvl`, `Datesold`, Count(*) AS `number` 
FROM Policyinfo 
WHERE `Product` = 'Outbacker-IDOL' && `Coverlvl` ='Gold' && `Datesold` >=
     ('2016-06-01')&& `Datesold` < ('2016-07-01') GROUP BY `Product`, `Coverlvl` 

它只返回一天的数据。当我需要的是在本月的其他日子返回0

enter image description here

1 个答案:

答案 0 :(得分:0)

删除groupby因为它只获得一个条目的数据

 SELECT `Product`,`Coverlvl`, `Datesold`, Count(*) AS `number` 
    FROM Policyinfo 
    WHERE `Product` = 'Outbacker-IDOL' && `Coverlvl` ='Gold' && `Datesold` >=
         ('2016-06-01')&& `Datesold` < ('2016-07-01') 

和&#34; 0&#34;无论你需要什么,你都可以使用&#34; case&#34; 案例的例子是

SELECT 
    customerName, state, country
FROM
    customers
ORDER BY (CASE
    WHEN state IS NULL THEN country
    ELSE 0
END);