我有3个表,如下:
Patron
======
patron_num
Booking_For_Schedule
====================
tname
date
time
booking_num
Booking_By_Patron
=================
booking_num
patron_num
我想检索包含patron_num
,date
,time
和tname
列的结果,如下所示:
patron_num date time tname
1 2013-11-03 20:00 TestName
...等
这个家庭作业问题的目的是教我们INNER JOINS,但我遇到了一些困难。某种SO用户可以把我推向正确的方向吗?
这是我的SQL:
SELECT `patron_num`,`date`,`time`,`tname`
FROM `booking_for_schedule` `F`
INNER JOIN `booking_by_patron` `B` on `F`.`booking_num` = `B`.`booking_num`
INNER JOIN `patron` `P` on `B`.`patron_num`=`P`.`patron_num`
返回错误:#1052 - Column 'patron_num' in field list is ambiguous
答案 0 :(得分:1)
SELECT `P`.`patron_num`,`date`,`time`,`tname`
FROM `booking_for_schedule` `F`
INNER JOIN `booking_by_patron` `B` on `F`.`booking_num` = `B`.`booking_num`
INNER JOIN `patron` `P` on `B`.`patron_num`=`P`.`patron_num`
将有助于模棱两可。
答案 1 :(得分:1)
由于patron_num
位于多个表中,因此您需要指定要使用的表。您可以通过在联接之前添加表别名来实现此目的。例如:
SELECT `B`.`patron_num`,`date`,`time`,`tname`
FROM `booking_for_schedule` `F`
INNER JOIN `booking_by_patron` `B` on `F`.`booking_num` = `B`.`booking_num`
INNER JOIN `patron` `P` on `B`.`patron_num`=`P`.`patron_num`