1

我有3张桌子,如下:

Patron
======
patron_num


Booking_For_Schedule
====================
tname
date
time
booking_num

Booking_By_Patron
=================
booking_num
patron_num

我想检索带有patron_numdatetime和列的结果tname,如下所示:

patron_num     date      time     tname
     1      2013-11-03  20:00    TestName 

...ETC

这个作业问题的目的是教我们 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

4

2 回答 2

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`

将有助于消除歧义。

于 2013-03-12T00:02:52.393 回答
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`
于 2013-03-12T00:03:12.550 回答