[英]Inner Joining the same table multiple times
So I have received this error: #1066 - Not unique table/alias: 'Purchase' 所以我收到了这个错误:#1066 - 不唯一的表/别名:'购买'
I am trying to do the following: 我正在尝试执行以下操作:
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";
Blank Table consists of: 空白表包括:
BlankCode,
IssueDate,
Status,
UserID,
BlankTypeCode
Payment Table consists of: 付款表包括:
Type,
Amount,
Status,
DueDate,
PurchaseID,
CurrencyRateDate
Purchase Table consists of: 购买表包括:
PurchaseID,
CustomerID,
PurchaseDate,
TotalFare,
TaxAmount,
CurrencyType,
Purchasecol
Ticket Table consists of: 票务表包括:
Price,
PurchaseID,
Blank_BlankCode,
Blank_BlankTypeCode,
TicketType,
Airline_Name
MCO_Blank Table consists of: MCO_Blank表包括:
Service,
Cost,
Description,
Purchase_PurchaseID,
Blank_BlankCode,
Blank_BlankTypeCode
I am unsure of how I can make this work. 我不确定如何才能完成这项工作。
You need to use table aliases. 您需要使用表别名。 You have mentioned the same table more than once in the
from
clause. 您在
from
子句中多次提到同一个表。 The query is something like this: 查询是这样的:
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";
I had to make a guess at which payment and purchase is intended for the aliases. 我不得不猜测哪些付款和购买是针对别名的。 These may not be correct in the
from
and where
clauses. 这些在
from
和where
子句中可能不正确。
您不能使用相同的名称多次连接表,因此要么使用inner join purchase p1 on...
别名inner join purchase p1 on...
或者同时使用两个连接谓词,如inner join purchase ON first predicate AND second predicate
You need a different alias for the table each time you use it. 每次使用时,表都需要不同的别名。
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"
You'll need to sort out which versions of the Purchase and Payment tables the selected columns come from, and also what should be in the WHERE clause really. 您需要确定所选列的哪些版本的Purchase和Payment表来自哪个,以及WHERE子句中应该包含哪些版本。
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";
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";
You have joined Payments table twice, If you do need to join it twice you need to Alias it , a different alias everytime you join this table. 您已加入Payments表两次,如果您确实需要加入两次,则需要Alias,每次加入此表时都会使用不同的别名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.