需要帮助连接sql

时间:2016-07-02 01:59:26

标签: mysql

我无法在我的评估中解决这个问题,需要一些帮助,

q)Michelle Bonnier预订的预订ID和入住和退房日期是什么? 使用全球假日数据库。

A)

select bookingid,guestid,firstname,lastname,checkindate,checkoutdate
from guests
join bookings on guests.GuestID = bookings.GuestID
where firstname = "Michelle"
and lastname = "Bonnier";

错误代码:1066。不是唯一的表/别名:'guests'

2 个答案:

答案 0 :(得分:1)

由于guestid位于select列列表的两个表中,我们需要说明要使用哪一个,所以试试这个:

SELECT bookingid,
       guests.guestid,
       firstname,
       lastname,
       checkindate,
       checkoutdate
FROM   guests
       JOIN bookings
         ON guests.guestid = bookings.guestid
WHERE  firstname = "michelle"
       AND lastname = "bonnier"  

答案 1 :(得分:1)

始终尝试使用表名别名

select 
    b.bookingid,g.guestid,g.firstname,g.lastname,
    b.checkindate,b.checkoutdate
    from guests g
    join bookings b on g.GuestID = b.GuestID
    where g.firstname = "Michelle"
    and g.lastname = "Bonnier";