如果为null,则MySql查询每月返回0

时间:2013-12-02 03:32:19

标签: mysql

如果我没有放置where子句,我的查询就可以了。但我必须有where子句,因为我将按用户获得每月总计。以下是我的询问:

SELECT IFNULL(count(PropertyId), 0) AS Total, a.monthLead
FROM (SELECT 'May' monthLead, 1 monthOrder UNION ALL SELECT 'Jun' monthLead, 2 monthOrder UNION ALL SELECT 'Jul' monthLead, 3 monthOrder UNION ALL SELECT 'Aug' monthLead, 4 monthOrder UNION ALL SELECT 'Sep' monthLead, 5 monthOrder UNION ALL SELECT 'Oct' monthLead, 6 monthOrder UNION ALL SELECT 'Nov' monthLead, 7 monthOrder UNION ALL SELECT 'Dec' monthLead, 8 monthOrder) a
LEFT JOIN leads b ON a.monthLead = DATE_FORMAT(b.DateEntered, '%b')
LEFT JOIN users_leads c ON c.LeadId = b.PropertyId
WHERE c.UserId = 78 AND b.IsRecordDeleted = 0
AND DATE_FORMAT(b.DateEntered, '%b') IN ('May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
GROUP BY a.monthLead, year(b.DateEntered), month(b.DateEntered)
ORDER BY a.monthOrder, b.DateEntered

输出是:

-----------------------
-- Total | monthLead --
-- 2     | May       --
-- 2     | Sep       --
-----------------------

应该是:

-----------------------
-- Total | monthLead --
-- 2     | May       -- 
-- 0     | Jun       --
-- 0     | Jul       --
-- 0     | Aug       --
-- 2     | Sep       --
-- 0     | Nov       --
-- 0     | Dec       --
-----------------------

我已将AND更改为OR,但这不正确。如果我使用OR,它将获得其他用户的潜在客户。

这是SQLFiddle:http://sqlfiddle.com/#!2/acf86/2

任何人都可以帮助我吗?

由于

2 个答案:

答案 0 :(得分:0)

尝试一下:

SELECT IFNULL(count(c.PropertyId), 0) AS Total, a.monthLead
FROM (SELECT 'May' monthLead, 1 monthOrder UNION ALL SELECT 'Jun' monthLead, 2 monthOrder UNION ALL SELECT 'Jul' monthLead, 3 monthOrder UNION ALL SELECT 'Aug' monthLead, 4 monthOrder UNION ALL SELECT 'Sep' monthLead, 5 monthOrder UNION ALL SELECT 'Oct' monthLead, 6 monthOrder UNION ALL SELECT 'Nov' monthLead, 7 monthOrder UNION ALL SELECT 'Dec' monthLead, 8 monthOrder) a
LEFT JOIN leads b ON a.monthLead = DATE_FORMAT(b.DateEntered, '%b')
LEFT JOIN users_leads c ON c.PropertyId = b.PropertyId AND c.UserId = 78 AND b.IsRecordDeleted = 0
AND DATE_FORMAT(b.DateEntered, '%b') IN ('May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
GROUP BY a.monthLead, year(b.DateEntered), month(b.DateEntered)
ORDER BY a.monthOrder, b.DateEntered

我所做的只是将where更改为andcount(c.PropertyId)更改为count(b.PropertyId)

作为旁注,这不会返回与上述查询相同的值,并且更容易阅读吗?

SELECT COUNT(ul.PropertyId) Total, m.monthLead
FROM (
  SELECT 'May' monthLead, 5 monthOrder UNION ALL
  SELECT 'Jun', 6 UNION ALL
  SELECT 'Jul', 7 UNION ALL
  SELECT 'Aug', 8 UNION ALL
  SELECT 'Sep', 9 UNION ALL
  SELECT 'Oct', 10 UNION ALL
  SELECT 'Nov', 11 UNION ALL
  SELECT 'Dec', 12) m
LEFT JOIN leads l ON m.monthOrder = month(l.DateEntered) AND l.IsRecordDeleted = 0 AND month(l.DateEntered) >= 5
LEFT JOIN users_leads ul ON ul.PropertyId = l.PropertyId AND ul.UserId = 78
GROUP BY m.monthOrder, year(l.DateEntered)
ORDER BY m.monthOrder, l.DateEntered

答案 1 :(得分:0)

这应该是你想要的:

SELECT IFNULL(count(b.PropertyId), 0) AS Total, a.monthLead
FROM (SELECT 'May' monthLead, 1 monthOrder UNION ALL SELECT 'Jun' monthLead, 2 monthOrder UNION ALL SELECT 'Jul' monthLead, 3 monthOrder UNION ALL SELECT 'Aug' monthLead, 4 monthOrder UNION ALL SELECT 'Sep' monthLead, 5 monthOrder UNION ALL SELECT 'Oct' monthLead, 6 monthOrder UNION ALL SELECT 'Nov' monthLead, 7 monthOrder UNION ALL SELECT 'Dec' monthLead, 8 monthOrder) a
LEFT JOIN (
  SELECT b.DateEntered, b.PropertyId /*, c.UserId, b.IsRecordDeleted -- not necessary*/
  FROM leads b, users_leads c
  WHERE c.PropertyId = b.PropertyId
  AND c.UserId = 78 AND b.IsRecordDeleted = 0
  AND DATE_FORMAT(b.DateEntered, '%b') IN ('May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
) b ON a.monthLead = DATE_FORMAT(b.DateEntered, '%b')
GROUP BY a.monthLead, year(b.DateEntered), month(b.DateEntered)
ORDER BY a.monthOrder, b.DateEntered

小提琴:http://sqlfiddle.com/#!2/acf86/35

另外需要注意的是,以下行不是必需的:

AND DATE_FORMAT(b.DateEntered, '%b') IN ('May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

因为记录将被a.monthLead = DATE_FORMAT(b.DateEntered, '%b')过滤掉。

基本的语义理念是要非常小心你所加入的内容。在我的查询中,我只使用了一个左连接,因为我想内连接输入表并确保当没有来自潜在客户的记录时,将没有来自user_leads的记录

此结果随后与您的查询中表A保持连接,以便在内部联接查询中没有记录时生成 null 记录。