我有一个包含两个表的数据库。第一个('日历')只包含一系列日期,例如' 2012-12-25'走向未来。这是由程序产生的。第二个(' new_allocations')包含一系列股票分配。示例内容如下所示:
+----+-------------+------------+----------+------------+
| id | delivery_ID | date | quantity | product_ID |
+----+-------------+------------+----------+------------+
| 1 | 1 | 2012-11-09 | 5 | 2 |
| 2 | 1 | 2012-11-08 | 5 | 2 |
| 3 | 2 | 2012-11-07 | 5 | 2 |
| 4 | 3 | 2012-11-06 | 5 | 2 |
| 5 | 4 | 2012-11-03 | 2 | 2 |
| 6 | 4 | 2012-11-02 | 5 | 2 |
| 7 | 5 | 2012-11-03 | 3 | 2 |
| 8 | 6 | 2012-11-05 | 5 | 2 |
| 9 | 7 | 2012-11-07 | 55 | 5 |
| 10 | 7 | 2012-11-06 | 34 | 5 |
| 11 | 7 | 2012-11-05 | 40 | 5 |
+----+-------------+------------+----------+------------+
以下查询(基本上是指'显示所提供日期范围内的天数列表,不包括星期日,当天共有库存分配')几乎是我想要的,除了它适用于所有产品(以' product_ID'表示)。
select datefield as Date, sum(ifnull(quantity,0)) as Qty from calendar left join new_allocations on (new_allocations.date=calendar.datefield) where (calendar.datefield>='2012-10-29' and calendar.datefield<='2012-11-10') and dayname(calendar.datefield) != 'Sunday' group by datefield;
+------------+------+
| Date | Qty |
+------------+------+
| 2012-10-29 | 0 |
| 2012-10-30 | 0 |
| 2012-10-31 | 0 |
| 2012-11-01 | 0 |
| 2012-11-02 | 5 |
| 2012-11-03 | 5 |
| 2012-11-05 | 45 |
| 2012-11-06 | 39 |
| 2012-11-07 | 60 |
| 2012-11-08 | 5 |
| 2012-11-09 | 5 |
| 2012-11-10 | 0 |
+------------+------+
所以我的问题是,只要我添加&#34;和product_ID =&#39; 2&#39;&#34;查询停止返回范围内的所有日期:
新查询:
select datefield as Date, sum(ifnull(quantity,0)) as Qty from calendar left join new_allocations on (new_allocations.date=calendar.datefield) where (calendar.datefield>='2012-10-29' and calendar.datefield<='2012-11-10') and dayname(calendar.datefield) != 'Sunday' and product_ID='2' group by datefield;
新结果:
+------------+------+
| Date | Qty |
+------------+------+
| 2012-11-02 | 5 |
| 2012-11-03 | 5 |
| 2012-11-05 | 5 |
| 2012-11-06 | 5 |
| 2012-11-07 | 5 |
| 2012-11-08 | 5 |
| 2012-11-09 | 5 |
+------------+------+
事实上我想要的是这样的:
+------------+------+
| Date | Qty |
+------------+------+
| 2012-10-29 | 0 |
| 2012-10-30 | 0 |
| 2012-10-31 | 0 |
| 2012-11-01 | 0 |
| 2012-11-02 | 5 |
| 2012-11-03 | 5 |
| 2012-11-05 | 5 |
| 2012-11-06 | 5 |
| 2012-11-07 | 5 |
| 2012-11-08 | 5 |
| 2012-11-09 | 5 |
| 2012-11-10 | 0 |
+------------+------+
我一直在向前和向后通过这个并且无法正确地说出查询 - 我错过了什么?
非常感谢提前。
CREATE TABLE `new_allocations` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`delivery_ID` int(11) DEFAULT NULL,
`date` date DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
`product_ID` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `new_allocations` (`id`, `delivery_ID`, `date`, `quantity`, `product_ID`)
VALUES
(1,1,'2012-11-09',5,2),
(2,1,'2012-11-08',5,2),
(3,2,'2012-11-07',5,2),
(4,3,'2012-11-06',5,2),
(5,4,'2012-11-03',2,2),
(6,4,'2012-11-02',5,2),
(7,5,'2012-11-03',3,2),
(8,6,'2012-11-05',5,2),
(9,7,'2012-11-07',55,5),
(10,7,'2012-11-06',34,5),
(11,7,'2012-11-05',40,5);
日历:
DROP TABLE IF EXISTS `calendar`;
CREATE TABLE `calendar` (
`datefield` date DEFAULT NULL
)
填写日历的程序:
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `fill_calendar`(start_date DATE, end_date DATE)
BEGIN
DECLARE crt_date DATE;
SET crt_date=start_date;
WHILE crt_date < end_date DO
INSERT INTO calendar VALUES(crt_date);
SET crt_date = ADDDATE(crt_date, INTERVAL 1 DAY);
END WHILE;
END;;
DELIMITER ;
答案 0 :(得分:1)
将您的条件添加到ON
子句中,而不是WHERE
子句:
SELECT datefield AS Date, COALESCE(SUM(quantity), 0) AS Qty
FROM calendar
LEFT JOIN
new_allocations
ON new_allocations.date = calendar.datefield
AND product_ID = '2'
WHERE calendar.datefield BETWEEN '2012-10-29' AND '2012-11-10'
AND dayname(calendar.datefield) != 'Sunday'
GROUP BY
datefield
WHERE
过滤了加入的结果。如果给定日期没有分配,则左连接将为日期生成一条记录,其中所有new_allocations
字段(包括productId
)设置为NULL
。