0

我有一个包含两个表的数据库。第一个(“日历”)仅包含未来的一系列日期,例如“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 |
+------------+------+

所以我的问题是,只要我添加“and product_ID='2'”,查询就会停止返回该范围内的所有日期:

新查询:

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 ;
4

1 回答 1

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

于 2012-11-03T21:52:01.760 回答