简体   繁体   English

如何从另一个表列中选择一个表?

[英]How do I select a table from another table column?

I have a table for seats at a venue but there are multiple venues.我在一个场地有一张座位表,但有多个场地。 I could create one long table with an id for each venue next to each seat but that could make a 2000 long mixed table of venues and seats.我可以为每个座位旁边的每个场地创建一个带有 id 的长桌,但这可以制作一个 2000 长的场地和座位混合桌。 So I thought I would create one table for each venue containing their own seating.所以我想我会为每个场地创建一张桌子,里面有自己的座位。 Such as:如:

tickets

id | venue_name | seat_id

venueA

seat_id    | section    |    row | seat_number

So I'd like to select the venue table from venue_name ("venueA") and the seat_id.所以我想从venue_name(“venueA”)和seat_id中选择场地表。 Such as

SELECT seat_id from (select venue from tickets(venue_name));

What I'm trying to capture is the seat_id for a venue so I know which section, row and seat is being requested.我试图捕获的是场地的座位 ID,所以我知道正在请求哪个部分、行和座位。 The section, row and seat information, as well as the number of seats, can be different for each venue.每个场地的部分、行和座位信息以及座位数量可能不同。

I need to be able to identify a seat at VenueA while keeping that separate from the same seat at VenueB.我需要能够识别 VenueA 的座位,同时将其与 VenueB 的同一个座位分开。

If that makes sense.如果这是有道理的。 This is my first foray into doing sql beyond simple design and interfaces to other people's work.这是我第一次尝试做 sql,超越简单的设计和与其他人工作的接口。 Specifically I'm using postgresql.具体来说,我正在使用 postgresql。

Your design is badly done, which is why you're running into this issue.您的设计做得不好,这就是您遇到此问题的原因。 Instead of separate tables for each venue, you should simply have a table of venues.您应该简单地拥有一张场地表,而不是为每个场地配备单独的桌子。 The seats table can then have a venue ID along with the seat information.座位表然后可以具有场地ID以及座位信息。 Something like:就像是:

Venues (venue_id, venue_name, etc.)
Seats (venue_id, seat_id, section, row, seat_number)
Tickets (venue_id, seat_id, ticket_id, etc.)

Instead of "ticket_id" for Tickets, you might want to just have an "event_id" that relates to your events and that should uniquely identify any ticket assuming that your system doesn't have any strange business requirements.您可能只想拥有一个与您的事件相关的“event_id”,而不是“ticket_id”,假设您的系统没有任何奇怪的业务需求,它应该唯一标识任何票证。 Also think about the possibility of non-reserved seating if that applies - a ticket might not actually apply to a "seat".如果适用,还要考虑非预订座位的可能性 - 机票实际上可能不适用于“座位”。 That would change the architecture of the tables as well.这也会改变表的架构。

If you're going to be doing more of this sort of database design then you should definitely take time to read up on database design and normalization.如果您要进行更多此类数据库设计,那么您绝对应该花时间阅读数据库设计和规范化。 It's not a trivial thing and the effects of a poor (or good) database design are huge on software development.这不是一件小事,糟糕(或良好)的数据库设计对软件开发的影响是巨大的

I would suggest a schema that included such tables like Venue, Seat, Event and Ticket.我会建议一个架构,其中包含诸如 Venue、Seat、Event 和 Ticket 之类的表。 I would model Ticket such that it had an EventId and SeatId, an Event would contain a VenueId, a seat would contain a VenueId also.我会对Ticket 进行建模,使其具有EventId 和SeatId,一个Event 将包含一个VenueId,一个座位也将包含一个VenueId。

--- edit to include explanation around testing --- --- 编辑以包括有关测试的解释 ---

Apologies if this sounds derogatory, but you can use Explain in PostGre to check the performance of your queries and optimise where appropriate.如果这听起来有贬义,请道歉,但您可以使用 PostGre 中的 Explain 来检查查询的性能并在适当的时候进行优化。 I would load the DB up with dummy data, possibly have indexes on EventId and VenueId in your child tables to begin with.我会用虚拟数据加载数据库,开始时可能在子表中的 EventId 和 VenueId 上有索引。 The documentation for EXPLAIN is here http://www.postgresql.org/docs/8.1/static/sql-explain.html EXPLAIN 的文档在这里http://www.postgresql.org/docs/8.1/static/sql-explain.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM