2012-11-03 43 views
0

我有一个数据库有两个表。第一个('日历')只包含一系列像'2012-12-25'这样的日期。这是由程序产生的。第二个('new_allocations')包含一系列股票分配。示例内容如下:MySQL的查询 - 我需要一些帮助,提炼它

+----+-------------+------------+----------+------------+ 
| 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 | 
+------------+------+ 

所以我的问题是,一旦我添加“和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 ; 

回答

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的日期产生一个日期记录。

+0

[先前的评论被删除] –

+0

非常感谢你的确。这正是我想要的,我永远不会到达那里。 (我会将它标记为答案,当它让我 - 这仍然是一个非常新的Q.)祝你有美好的一天。 –