Mysql 如何查询可用项目?

Mysql 如何查询可用项目?,mysql,Mysql,我想知道是否有人可以帮助我查询mysql数据库的可用性 所以,我有两张桌子:tbl_项目SUnit项目和tbl_预订 tbl_预订 -条形码 -保留日期 -时间起点 -时间终点 待定项目 -条形码 -项目名称 -itemDesc 我想做的是这样的请参考图片: 到目前为止,我的问题是: SELECT a.bcode, b.resdate, b.timestart, b.timeend FROM tbl_items a INNER JOIN tbl_reservations b on a.bcod

我想知道是否有人可以帮助我查询mysql数据库的可用性

所以,我有两张桌子:tbl_项目SUnit项目和tbl_预订

tbl_预订 -条形码 -保留日期 -时间起点 -时间终点

待定项目 -条形码 -项目名称 -itemDesc

我想做的是这样的请参考图片:

到目前为止,我的问题是:

SELECT a.bcode, b.resdate, b.timestart, b.timeend
FROM tbl_items a
INNER JOIN tbl_reservations b
on a.bcode=b.bcode
WHERE a.bcode
not in
(
  Select bcode
  FROM tbl_reservations
  WHERE bcode not in
  (
  SELECT bcode
  FROM tbl_reservations
  where resdate='2013-09-25' AND retdate is null
  )
AND ((timestart < '10:00'AND timeend < '10:00')
AND (timestart > '15:00' AND timeend > '15:00'))
)

但即使有这么长的a代码,它仍然允许一些已经预订或不可用的设备出现

有什么帮助吗

谢谢D

resultset的屏幕截图,带有我不想包含在resultset中的保留:

突出显示的是与上面代码中的条件重叠的那些

更新:这是新代码。。。我很惊讶,因为它返回的行在10:00之前同时包含timestart和timeend

select rsv.controlno, rsv.bcode, rsv.resdate, rsv.timestart, rsv.timeend
FROM
(
  select bcode
  from tbl_items
) i
inner join tbl_reservations rsv
on i.bcode=rsv.bcode
WHERE resdate='2013-09-25' AND NOT (rsv.timeend > '10:00' AND rsv.timestart < '15:00');

请尝试下面的代码。我相信用AND换OR就行了。外部和要求其中一个条件为真。期间内或期间外的预订,但期间内无预订

SELECT a.bcode, b.resdate, b.timestart, b.timeend
FROM tbl_items a
INNER JOIN tbl_reservations b
on a.bcode=b.bcode
WHERE a.bcode
not in
(
  Select bcode
  FROM tbl_reservations
  WHERE bcode not in
  (
  SELECT bcode
  FROM tbl_reservations
  where resdate='2013-09-25' AND retdate is null
  )
AND 
    ((timestart < '10:00' AND timeend < '10:00')
    OR (timestart > '15:00' AND timeend > '15:00')

    )
)

在一定的时间范围内关闭AND或

它仍然允许一些已经预订或不可用的设备出现。-你能给出一些例子,这样我们就可以帮助调试了吗?作为第一步,您可能希望在和周围放置额外的括号。。。或所以它肯定有正确的优先级。结果集和更新的代码@Rup..试试这个链接:谢谢你的回复,事实上,我不能允许那种跨越整个时间范围但不从内部开始或结束的方式…太好了,我将删除“span”选项。或是否满足您的需要?或仍然没有进行足够的筛选。。。仍然允许重叠的。。。谢谢你的回复,我有了一个新的代码,它比我以前的代码过滤得更好我会把它寄出去。