我有两张桌子如上所示。 让我们称之为Table1和Table2
我们得到结果表。这给了累积的结果。
SELECT T1.c_id,T1.p_name, T2.Date, IF(T2.Date IS NULL,0,COUNT(COALESCE(T2.count_p_id, 0))) AS count_p_id
FROM (
SELECT a.company_id, b.id, a.p_id, b.name
FROM table1 a, product_info_table b
WHERE a.p_id = b.p_id
AND a.c_id = 15
) AS T1
LEFT JOIN (
SELECT c_id,p_id, Date, COUNT(p_id) AS count_p_id
FROM table2
WHERE Date BETWEEN '2016-01-01' AND '2016-01-31'
GROUP BY DATE(Date)
) AS T2 ON T2.p_id = T1.p_id
GROUP BY DATE(T2.Date)
问题是所有日期都没有显示,只有table2中存在的日期显示。我需要在日期范围内显示所有日期,并将其显示为' 0'
这没有给出正确的结果。我究竟做错了什么?任何帮助都会很好吗?
我正在使用mySQL。