我是一个相对新手,我已经与这个斗争了太久了 - 所以请任何人都可以帮忙!
我有一张表格,可以接收不同类型开发的查询(dev_enquiry),我正在创建一个动态图表来显示每个开发每天有多少查询。我加入了日历表,其中包含一列日期,以便在没有查询时可以获得 0 值。
这个 SQL 给了我我想要的东西,但它适用于所有的发展
SELECT calendar.datefield AS DATE, COUNT(de_id) AS enquiries
FROM `calendar`
LEFT JOIN dev_enquiry ON DATE(dev_enquiry.de_date_created) = calendar.datefield)
WHERE calendar.datefield BETWEEN (
SELECT MIN(DATE(de_date_created))
FROM dev_enquiry
)
AND (SELECT MAX(DATE(de_date_created)) FROM dev_enquiry)
GROUP BY DATE
ORDER BY DATE DESC
2012-11-08 3
2012-11-07 0
2012-11-06 2
2012-11-05 1
2012-11-04 3
我想按开发类型过滤掉结果,但当那天没有针对特定开发的查询时仍保留空值。
以下过滤结果但不给我空值:
SELECT cal.datefield AS DATE, COUNT(t.de_id) AS enquiries
FROM (
SELECT * FROM dev_enquiry as d LEFT JOIN calendar as c ON (DATE(d.de_date_created) = c.datefield)
WHERE d.de_dev_name = 'Green') as t
LEFT JOIN calendar as cal ON (cal.datefield = DATE(t.de_date_created))
WHERE (cal.datefield BETWEEN (SELECT MIN(DATE(t.de_date_created))
FROM dev_enquiry) AND (SELECT MAX(DATE(de_date_created)) FROM dev_enquiry))
GROUP BY DATE ORDER BY DATE DESC
2012-11-08 1
2012-11-06 2
2012-11-05 1
2012-11-04 1
缺少 2012-11-07 的结果