如何使用其他表中的数据创建此视图

时间:2018-04-24 11:27:19

标签: mysql sql view

我有一个带有下表的mysql数据库。

游览表

Excursion ID    |  Port ID   |    Excursion Name

预订表

Booking ID  |  User ID  |  Excursion ID  |  Number of People  | Event Date

我希望能够在预订表中给出某个UserID时创建一个视图,如下图所示。

Booking ID   |  Excursion Name  |  Excursion  ID    |  Number of People

3 个答案:

答案 0 :(得分:0)

CREATE VIEW `yourDB`.`yourViewName` AS
SELECT 
    `a`.`BookingID` AS `BookingID`,
    `b`.`ExcursionName` AS `ExcursionName`,
    `b`.`ExcursionID` AS `ExcursionID`,
    `a`.`NumberOfPeople` AS `NumberOfPeople`
FROM
    (`yourDB`.`BookingsTable` `a`
    JOIN `yourDB`.`ExcursionsTable` `b`)
WHERE
    (`a`.`UserID` = 'YourValue')

希望这能解决您的问题。

答案 1 :(得分:0)

试试这个:

CREATE VIEW Your_View_Name AS
SELECT BT.Booking_ID, ET.Excursion_Name, ET.Excursion_ID,BT.Number_of_People
FROM Excursion_Table ET
JOIN Bookings_Table BT ON ET.Excursion_ID = BT.Excursion_ID
WHERE BT.User ID = ????;

答案 2 :(得分:0)

您可以尝试使用此脚本:

CREATE VIEW ViewName AS 
    SELECT
        B.Booking_ID ,  E.Excursion_Name  ,  E.Excursion_ID   ,  B.Number_of_People
    FROM
        Excursion  E
    LEFT JOIN Bookings B ON (E.Excursion_ID = B.Excursion_ID)