在SQL中加入许多表

时间:2015-05-05 07:56:23

标签: mysql sql sql-server inner-join

我正试图解决这个SQL问题:  酒店连锁的数据库包含以下表格:

    Hotel(HotelNo, HotelName, City)   
    Room(RoomNo, HotelNo, Type, Price)  
    Booking(HotelNo, GuestNo, DateFrom, DateTo, RoomNo)     
    Guest(GuestNo,  GuestName, GuestAddress)

我想列出Grosvenor Hotel酒店所有客房的详细信息,包括入住客房的客人的姓名,如果房间被占用的话。

我可以在SQL中加入2个表,但我不知道如何加入4个表。

我的尝试可能是:

SELECT Room.*, Guest.GuestName 
    FROM Room
    INNER JOIN Hotel, Booking, Guest
    ON Hotel.HotelName = "Grosvenor Hotel", Hotel.HotelNo = Room.HotelNo, Booking.GuestNo = Guest.GuestNo;

我认为这完全错了,但无论如何,希望有人知道我应该做什么。提前致谢

3 个答案:

答案 0 :(得分:0)

正确的语法是:

SELECT Room.*, Guest.GuestName 
FROM Room
INNER JOIN Hotel on Hotel.HotelNo = Room.HotelNo, 
inner join Booking on Booking.hotelno= Hotel.HotelNo
inner join Guest on Booking.GuestNo = = Guest.GuestNo
where Hotel.HotelName = "Grosvenor Hotel"

答案 1 :(得分:0)

试试这个:

  select g.roomno, g.guestname 

  from hotel h join room r on h.hotelno = r.hotelno
   join booking b on b.hotelno=r.hotelno 
   join guest g on g.guestno=b.guestno

  where h.hotelname='Grosvenor Hotel';

答案 2 :(得分:0)

你也可以尝试这个..它会帮助你

SELECT Room.*, Guest.GuestName FROM Room
INNER JOIN Hotel on Hotel.HotelNo = Room.HotelNo, 
join Booking on Booking.HotelNo= Hotel.HotelNo
join Guest on Booking.GuestNo = Guest.GuestNo
where Hotel.HotelName = "Grosvenor Hotel"