3

我一直在努力进行查询,我不确定它是否可能在纯 sql 中或需要 php 支持。

基本上我想生成一个订单报告,列出系统中超额预订的产品。

表结构:

订单

id int
bookingStart DATETIME
bookingEnd DATETIME

order_lines

id int
qty int
product_id int
booking_id int

产品

id int
stock int

一个订单可以包含具有相同产品的多行。因此,订单上的每个产品都需要有一个 SUM。有没有人对实现这一目标的最佳方法有任何建议?

我尝试使用子查询在特定时间间隔内循环,但它没有考虑不同预订之间的重叠,如果可以通过单个查询完成,我有点卡在这里:

SELECT  (SELECT SUM(lin2.qty) FROM booking_lines lin2,orders b2
                       WHERE
                         lin2.product_id=e.id    AND
                       b2.id=lin2.booking_id  AND                           (
                       (b2.bookingStart BETWEEN b1.bookingStart AND b1.bookingEnd) OR
                       (b2.bookingEnd BETWEEN b1.bookingStart AND b1.bookingEnd) OR
                       (b2.bookingStart < b1.bookingStart AND b2.bookingEnd >     b1.bookingEnd)                           
                       )    as booked SUM(lin1.qty) 
                 FROM orders b1
                 LEFT JOIN order_lines lin1 ON b1.id=lin1.booking_id
                 LEFT JOIN products e ON e.id=lin1.product_id

                (
                    (b1.bookingStart BETWEEN '$s' AND '$e') OR
                    (b1.bookingEnd BETWEEN '$s' AND '$e') OR
                    (b1.bookingStart < '$s' AND b1.bookingEnd > '$e') 
        )
         GROUP BY b1.id,lin1.product_id

问题 如果我们的产品 x 库存为 10。

订单 1 已预订 3 个产品 x 订单 2 已预订 5 个产品 x 订单 3 已预订 8 个产品 x

下面的行表示预订的时间跨度

_ _ ___订单 1 x:3 _ __ _
       _ __订单 2 x:5 _ __ _ _
_ __ _ __ _ ___订单 3 x:8 _ __ _ __
                                            __订单 4 x:2 _

因此,只有当 3 个订单重叠时,产品 x 才实际超额预订,我不知道如何创建能够检测到该问题的查询。

4

2 回答 2

1

表结构

CREATE TABLE products (
    product_id INT AUTO_INCREMENT PRIMARY KEY,
    name       VARCHAR(255),
    stock      INT
) ENGINE=InnoDB;

CREATE TABLE orders (
    order_id     INT AUTO_INCREMENT PRIMARY KEY,
    bookingStart DATETIME,
    bookingEnd   DATETIME
) ENGINE=InnoDB;


CREATE TABLE order_lines (
    line_id       INT AUTO_INCREMENT PRIMARY KEY,
    order_id      INT,
    product_id    INT,
    qty           INT NOT NULL DEFAULT 0,
    CONSTRAINT FOREIGN KEY(order_id)   REFERENCES orders(order_id) ON DELETE RESTRICT,
    CONSTRAINT FOREIGN KEY(product_id) REFERENCES products(product_id) ON DELETE RESTRICT
) ENGINE=InnoDB;


INSERT INTO products(name, stock) VALUES('Product 1', 8);
INSERT INTO products(name, stock) VALUES('Product 2', 14);
INSERT INTO products(name, stock) VALUES('Product 3', 25);

INSERT INTO orders(bookingStart,bookingEnd) VALUES(NOW(), (NOW() + INTERVAL 2 HOUR));
INSERT INTO orders(bookingStart,bookingEnd) VALUES(NOW(), (NOW() + INTERVAL 3 HOUR));
INSERT INTO orders(bookingStart,bookingEnd) VALUES(NOW(), (NOW() + INTERVAL 4 HOUR));

INSERT INTO order_lines(order_id, product_id, qty) VALUES(1, 1, 2);
INSERT INTO order_lines(order_id, product_id, qty) VALUES(1, 1, 8);
INSERT INTO order_lines(order_id, product_id, qty) VALUES(2, 2, 5);
INSERT INTO order_lines(order_id, product_id, qty) VALUES(2, 2, 10);
INSERT INTO order_lines(order_id, product_id, qty) VALUES(3, 3, 2);
INSERT INTO order_lines(order_id, product_id, qty) VALUES(3, 3, 8);
INSERT INTO order_lines(order_id, product_id, qty) VALUES(3, 3, 10);

要获得超额预订的产品:

SELECT
  products.*,
  SUM(order_lines.qty) as sum_qty
FROM products
LEFT JOIN order_lines ON order_lines.product_id = products.product_id
LEFT JOIN orders ON orders.order_id = order_lines.order_id
WHERE orders.bookingStart >= '2013-06-12' 
AND orders.bookingEnd <= '2013-06-14'
GROUP BY order_lines.order_id, order_lines.product_id
HAVING sum_qty > products.stock

将返回Product 1and Product 2,因为qtyin的总和order_lines大于products.stock(这可能是产品的可用数量?)。

SqlFiddle

于 2013-06-11T22:01:26.267 回答
1

我没有要测试的 MySql 实例,但你有没有尝试过这样的事情:

select p.id, p.stock, t.bookedQty
from products as p
    inner join (
            select ol.product_id, sum(ol.qty) as bookedQty
            from order_lines as ol
                inner join orders as o on ol.booking_id = o.id
            where o.bookingStart between '2013-06-10' and '2013-06-11'
                    and t.bookedQty > p.stock
            group by ol.product_id) as t

--见评论 1 --

我使用了下面 tftd 答案中的 SqlFiddle 架构(谢谢)。这应该每天为接下来的 100 个超额预订、预订数量和库存数量生成:

SELECT
        date,
        p.product_id,
        sum(ol.qty) AS total_booked,
        p.stock AS available
FROM  (
        SELECT c.date
        FROM
          (SELECT curdate() + interval (a.a + (10 * b.a)) DAY AS date
           FROM
             (SELECT 0 AS a
              UNION ALL SELECT 1
              UNION ALL SELECT 2
              UNION ALL SELECT 3
              UNION ALL SELECT 4
              UNION ALL SELECT 5
              UNION ALL SELECT 6
              UNION ALL SELECT 7
              UNION ALL SELECT 8
              UNION ALL SELECT 9) AS a CROSS
           JOIN
             (SELECT 0 AS a
              UNION ALL SELECT 1
              UNION ALL SELECT 2
              UNION ALL SELECT 3
              UNION ALL SELECT 4
              UNION ALL SELECT 5
              UNION ALL SELECT 6
              UNION ALL SELECT 7
              UNION ALL SELECT 8
              UNION ALL SELECT 9) AS b
          ) AS c
        WHERE c.date >= curdate() AND c.date < DATE_ADD(curdate(), INTERVAL 100 DAY)
    ) AS gendates
    INNER JOIN orders AS o ON o.bookingStart <= gendates.date AND o.bookingEnd > gendates.date
    INNER JOIN order_lines AS ol ON o.order_id = ol.order_id
    INNER JOIN products AS p ON ol.product_id = p.product_id
GROUP BY gendates.date, p.product_id, p.stock
HAVING total_booked > p.stock

http://sqlfiddle.com/#!2/a5061e/25

日期生成链接

于 2013-06-11T21:38:22.090 回答