内部多次连接同一个表

时间:2014-02-08 02:44:32

标签: sql inner-join

所以我收到了这个错误:#1066 - 不是唯一的表/别名:'购买'

我正在尝试执行以下操作:

    SELECT Blank.BlankTypeCode
          ,Blank.BlankCode
          ,Payment.Amount
          ,Payment.Type
          ,Purchase.PurchaseDate
          ,Payment.DatePaid
FROM Blank
INNER JOIN Ticket
ON Blank.BlankCode = Ticket.Blank_BlankCode
INNER JOIN MCO_Blank
ON Blank.BlankCode = MCO_Blank.Blank_BlankCode
INNER JOIN Purchase
ON  Ticket.PurchaseID = Purchase.PurchaseID
INNER JOIN Purchase
ON MCO_Blank.PurchaseID = Purchase.PurchaseID
INNER JOIN Payment
ON Ticket.PurchaseID = Payment.PurchaseID
INNER JOIN Payment
ON MCO_Blank.PurchaseID = Payment.PurchaseID
WHERE Payment.Status = "Paid";

空白表包含:

BlankCode,
IssueDate,
Status,
UserID, 
BlankTypeCode

付款表包括:

Type,
Amount,
Status,
DueDate,
PurchaseID,
CurrencyRateDate

购买表包括:

PurchaseID,
CustomerID,
PurchaseDate,
TotalFare,
TaxAmount,
CurrencyType,
Purchasecol

票证表包括:

Price,
PurchaseID,
Blank_BlankCode,
Blank_BlankTypeCode,
TicketType,
Airline_Name

MCO_Blank表包含:

Service,
Cost,
Description,
Purchase_PurchaseID,
Blank_BlankCode,
Blank_BlankTypeCode

我不确定如何才能做到这一点。

5 个答案:

答案 0 :(得分:6)

您需要使用表别名。您在from子句中多次提到同一个表。查询是这样的:

SELECT b.BlankTypeCode, b.BlankCode, pa1.Amount, pa1.Type, p1.PurchaseDate, pa2.DatePaid
FROM Blank b
INNER JOIN Ticket t
ON b.BlankCode = t.Blank_BlankCode
INNER JOIN MCO_Blank mb
ON b.BlankCode = mb.Blank_BlankCode
INNER JOIN Purchase p1
ON  t.PurchaseID = p1.PurchaseID
INNER JOIN Purchase p2
ON mb.PurchaseID = p2.PurchaseID
INNER JOIN Payment pa1
ON t.PurchaseID = pa1.PurchaseID
INNER JOIN Payment pa2
ON mc.PurchaseID = pa2.PurchaseID
WHERE pa1.Status = "Paid";

我不得不猜测哪些付款和购买是针对别名的。这些可能在fromwhere条款中不正确。

答案 1 :(得分:2)

您不能使用相同的名称多次加入表格,因此要么使用inner join purchase p1 on...之类的别名加入表格,要么像inner join purchase ON first predicate AND second predicate一样使用两个连接谓词

答案 2 :(得分:1)

每次使用时,表格都需要不同的别名。

SELECT B.BlankTypeCode, B.BlankCode, A1.Amount, A1.Type, P1.PurchaseDate, P1.DatePaid
  FROM Blank AS B
  JOIN Ticket    AS T  ON B.BlankCode = T.Blank_BlankCode
  JOIN MCO_Blank AS M  ON B.BlankCode = M.Blank_BlankCode
  JOIN Purchase  AS P1 ON T.PurchaseID = P1.PurchaseID
  JOIN Purchase  AS P2 ON M.PurchaseID = P2.PurchaseID
  JOIN Payment   AS A1 ON T.PurchaseID = A1.PurchaseID
  JOIN Payment   AS A2 ON M.PurchaseID = A2.PurchaseID
 WHERE A1.Status = "Paid"
   AND A2.Status = "Paid"

您需要整理所选列的购买和付款表格的版本,以及WHERE子句中应该包含的内容。

答案 3 :(得分:1)

SELECT bl.BlankTypeCode ,bl.BlankCode ,pymt.Amount ,pymt.Type ,purc.PurchaseDate ,pymt.DatePaid FROM Blank bl INNER JOIN Ticket tk ON bl.BlankCode = tk.Blank_BlankCode INNER JOIN MCO_Blank mco_bl ON bl.BlankCode = mco_bl.Blank_BlankCode INNER JOIN Purchase purc ON tk.PurchaseID = purc.PurchaseID AND mco_bl.PurchaseID = purc.PurchaseID INNER JOIN Payment pymt ON tk.PurchaseID = pymt.PurchaseID AND mco_bl.PurchaseID = pymt.PurchaseID WHERE pymt.Status = "Paid";

答案 4 :(得分:0)

INNER JOIN Purchase
ON MCO_Blank.PurchaseID = Purchase.PurchaseID
INNER JOIN Payment                         --<-- 
ON Ticket.PurchaseID = Payment.PurchaseID
INNER JOIN Payment                         --<--
ON MCO_Blank.PurchaseID = Payment.PurchaseID
WHERE Payment.Status = "Paid";

您已加入Payments表两次,如果您确实需要加入两次,则需要Alias,每次加入此表时都会使用不同的别名。