SQL - 查找时间表中的空位
我正在使用 SQLite 数据库,我有三个表描述建筑物、房间和预定的事件。
这些表如下所示:
- Buildings(ID,Name)
- Rooms(ID,BuildingID,Number)
- Events(ID,BuildingID,RoomID,Days,s_time,e_time)
因此每个事件都与建筑物和房间相关联。 Days 列包含一个整数,它是与一周中的天对应的质数的乘积(值 21 表示事件发生在星期二 = 3 和星期四 = 7)。
我希望找到一种方法来生成特定建筑物中将在接下来的几个小时内开放的房间的报告,以及它们将开放多长时间。
这是我到目前为止所拥有的:
SELECT Rooms.Number
FROM Rooms
INNER JOIN Buildings on ( Rooms.BuildingID = Buildings.ID )
WHERE
Buildings.Name = "BuildingName"
EXCEPT
SELECT Events.RoomID
FROM Events
INNER JOIN Buildings on ( Events.BuildingID = Buildings.ID )
WHERE
Buildings.Name = "BuildingName" AND
Events.days & 11 = 0 AND
time("now", "localtime" BETWEEN events.s_time AND events.e_time;
在这里,我找到特定建筑物的所有房间,然后删除当前正在进行预定活动的房间。
我期待所有有用的提示/评论。
I am working with a SQLite database and I have three tables describing buildings,rooms and scheduled events.
The tables look like this:
- Buildings(ID,Name)
- Rooms(ID,BuildingID,Number)
- Events(ID,BuildingID,RoomID,Days,s_time,e_time)
So every event is associated with a building and a room. The column Days contains an integer which is a product of prime numbers corresponding to days of the week ( A value of 21 means the event occurs on Tuesday = 3 and Thursday = 7).
I am hoping to find a way to generate a report of rooms in a specific building that will be open in the next few hours, along with how long they will be open for.
Here is what I have so far:
SELECT Rooms.Number
FROM Rooms
INNER JOIN Buildings on ( Rooms.BuildingID = Buildings.ID )
WHERE
Buildings.Name = "BuildingName"
EXCEPT
SELECT Events.RoomID
FROM Events
INNER JOIN Buildings on ( Events.BuildingID = Buildings.ID )
WHERE
Buildings.Name = "BuildingName" AND
Events.days & 11 = 0 AND
time("now", "localtime" BETWEEN events.s_time AND events.e_time;
Here I find all rooms for a specific building and then I remove rooms which currently have an scheduled event in progress.
I am looking forward to all helpful tips/comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将日期存储为素数的乘积,则模 (%) 运算符可能更有用:
选择星期一或星期三发生的事件。
但我确实必须指出,存储素数的乘积在计算和存储上都非常昂贵。存储两个幂的总和(周一 = 1、周二 = 2、周三 = 4、周四 = 8、周五 = 16、周六 = 32、周日 = 64)要容易得多。
当前实施的最大可能值为 510,510。存储此类数字的最小数据类型是 int(每行 32 位),检索编码数据需要最多 7 次模 (%) 运算。
2^n 求和方法的最大可能值是 127,它可以存储在tinyint(每行 8 位)中,并且检索编码数据将使用按位与 (&),这更便宜(因此更快)。
对于您正在使用的内容来说可能不是问题,但是选择任何可以为您提供最佳空间和性能效率的方法是一个好习惯,以免您在更大规模地实施解决方案时遇到严重问题。
If you're storing dates as the product of primes, the modulo (%) operator might be more useful:
Would select events happening on either a Monday or Wednesday.
I do have to point out though, that storing the product of primes is both computationally and storage expensive. Much easier to store the sum of powers of two (Mon = 1, Tues = 2, Wed = 4, Thurs = 8, Fri = 16, Sat = 32, Sun = 64).
The largest possible value for your current implementation is 510,510. The smallest data type to store such a number is int (32 bits per row) and retrieving the encoded data requires up to 7 modulo (%) operations.
The largest possible value for a 2^n summation method is 127 which can be stored in a tinyint (8 bits per row) and retrieving the encoded data would use bitwise and (&) which is somewhat cheaper (and therefore faster).
Probably not an issue for what you're working with, but it's a good habit to choose whatever method gives you the best space and performance efficiency lest you hit serious problems should your solution be implemented at larger scales.