一尘不染

内部多次连接同一张表

sql

因此,我收到此错误:#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

我不确定如何进行这项工作。


阅读 223

收藏
2021-03-10

共1个答案

一尘不染

您需要使用表别名。您在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";

我不得不猜测别名应该使用哪种付款方式和购买方式。这些在fromandwhere子句中可能不正确。

2021-03-10