我只是想就我正在开发的设备库存和预订系统寻求帮助...
所以...我有 3 张 table 。 tbl_items
、tbl_bulk_items
和 tbl_reservations
。
tbl_items包含所有元素(按UNIT分类的装备)
tbl_items_bulk 包含所有散装商品。例如菜刀一套(12把/套)
tbl_reservations包含所有预订信息
到目前为止,这是我获取 tbl_items
和 tbl_items_bulk
表中所有项目的查询。
SELECT bcode
FROM
/*gets all the items in the inventory*/
(SELECT bcode FROM tbl_items AS T1
UNION
SELECT bcode FROM tbl_items_bulk) AS T2
我会针对另一个查询来查询它,以获取 tbl_reservation 中不存在的所有 bcode 的列表(意味着它可用)...
/*gets all the items in the inventory that satisfies the given conditions*/
WHERE bcode
NOT IN
(SELECT bcode FROM tbl_test)
但问题是,我需要使用条件(不太知道如何正确执行此操作)来进一步过滤查询以获取所有“可用”项目。
这是条件...
如果该商品的 bcode 在预订表中,则该商品不可用。 或者如果它(项目bcode)在预订表中,但预订的日期和时间与用户指定的不同,那么它仍然可以算作可用。
例如设备 1 已于 2013 年 9 月 16 日上午 7:30 至上午 10:30 预订。如果用户询问其他日期(例如 2013 年 9 月 17 日)从上午 7:30 到晚上 10:30 是否可用,它应该显示为“可用”。 (我希望我能说得更清楚)
关于如何在特定日期和时间获得所有“可用”设备有什么想法吗?
我当前的代码:
SELECT bcode
FROM
/*gets all the items in the inventory*/
(SELECT bcode FROM tbl_items AS T1
UNION
SELECT bcode FROM tbl_items_bulk) AS T2
/*gets all the items in the inventory that satisfies the given conditions*/
WHERE bcode
NOT IN
(SELECT bcode FROM tbl_test WHERE resDate!='2013-09-16')
2013 年 9 月 17 日更新:
最新代码:
SELECT b.*
FROM (SELECT * FROM tbl_items
UNION
SELECT * FROM tbl_items_bulk
) b left outer join
tbl_test t
on b.bcode = t.bcode and
NOT ('4:30' < t.timeSTART OR '7:00' > t.timeEND)
WHERE t.bcode is null;
最佳答案
听起来您想查看在某个时间是否有 bcode
的预订。我认为以下查询具有正确的逻辑:
SELECT b.bcode
FROM (SELECT bcode FROM tbl_items
UNION
SELECT bcode FROM tbl_items_bulk
) b left outer join
tbl_reservation r
on b.bcode = r.bcode and
@USERTO <= r.ToDate and
@USERFROM >= r.FromDate
WHERE r.bcode is null;
它的作用是使用左外连接
检查是否有与给定日期的bcode
匹配的预订。请注意,日期条件位于 on
子句中。因此,如果存在匹配,则将返回一行。如果没有匹配,则预订表中的 bcode
值将为 NULL
——这就是 where
子句过滤的内容。
关于mysql - 如何查询可用的商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18830521/