mysql中一对多显示记录

时间:2012-02-16 19:45:22

标签: mysql

我有2张桌子预订 BookingPassengers

我是否可以创建一个视图,该视图将显示单个记录的所有预订信息以及 BookingPassengers 中由 BookingID 链接的所有记录。

每个预订记录可以包含多个 BookingPassengers 记录

预订
BookingID
BookingStart
BookingEnd
BookingPrice

BookingPassengers
BookingID
名称
年龄

此致          马丁

1 个答案:

答案 0 :(得分:1)

一位乘客的所有预订信息:

  CREATE VIEW v1 AS SELECT Booking.*
                 FROM Booking
                 INNER JOIN BookingPassengers
                     ON Booking.BookingID = BookingPassengers.BookingID
                     and BookingPassengers.Name = 'John'

每次预订的所有信息:

  CREATE VIEW v2 AS SELECT Booking.BookingID, BookingPassengers.Name
                 FROM Booking
                 INNER JOIN BookingPassengers
                     ON Booking.BookingID = BookingPassengers.BookingID