仅检索可用座位?
我很困惑如何才能只退回尚未预订的座位。
我得到的表模式
在同一个表中存储了 2 个场馆的座位信息(每个场馆 50 个座位),
然后我有一个 SeatBooking 表,它采用 SeatNumber 场馆和预订号码,使其对于表演而言是唯一的。
常见的想法是:创建一个名为 SeatStatus 的字段,其布尔数据类型设置为 True(如果已采取)和 False(如果可用)。然后查询返回所有 SeatStatus = false 的位置。
在这种情况下可以这样做吗?
I am confused about how I could return only the seats that have not already been booked.
The table schema I have been given
Stores 2 venues of seat information in the same table (50 seats each)
I then have a SeatBooking table which takes the seatNumber Venue and Booking number to make it unique to a performance.
The idea have seen a lot is: Create a field called SeatStatus with a boolean data type set to True if Taken and False if available. Then do the query return all where SeatStatus = false.
Can this be done in this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不特定于您正在使用的语言,但我会使用 SQL LEFT JOIN 来执行此操作。
基本上,这种类型的连接表示您知道左侧存在(在本例中为座位表),但右侧(预订信息)可能不存在,因此您可能会得到空值。将“座位信息”表左连接到“座位预订”表,选择您要查找信息的演出。最后一个条件,检查预订信息是否为空,如果为空,则说明座位尚未预订。
如果您的 SQL 表按照应有的方式进行了优化,那么这会相当快,并且您将得到的结果是,只有未保留的席位,因此您不需要在代码中进行任何过滤,并且可以让 SQL 服务器执行以下操作为您承担繁重的工作。 (这通常是最有效的方法)
快速完成此操作所需的索引是预订信息表上的座位号和性能。
以下是此查询的一般形式:
首先尝试不使用“AND book.performance is null”,然后选择预订表以确保您获得所有数据,而且重要的是,每个座位只有一行数据。
This isn't specific to the language you're doing, but I would use an SQL LEFT JOIN to do this.
Basically this type of join says that you know the left side is present, (In this case, the seating chart) but the right side (The booking information) might not be, so you may get nulls. Left Join the Seat Information table to the Seat Booking table, selecting the performance you're looking up information for. As a final condition, check if the booking information is null, because if it is, the seat has not been reserved.
This is quite fast if your SQL tables are optimized the way they should be, and you'll get as a result, ONLY unreserved seats, so you don't need to do any filtering in code, and can have the SQL server do the heavy lifting for you. (Which is generally the most efficient way to do this)
The index you need to make this fast is on Seat Number and Performance on the Booking Information table.
Here's a general form of this query:
Try this without the 'AND book.performance is null' first, and selecting the booking table as well to make sure you're getting all the data, and, importantly, only one row per seat.