gpt4 book ai didi

sql - 多次内连接同一张表

转载 作者:行者123 更新时间:2023-12-02 01:35:15 26 4
gpt4 key购买 nike

所以我收到了这个错误:#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

我不确定如何才能完成这项工作。

最佳答案

您需要使用表别名。您在 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 子句中可能不正确。

关于sql - 多次内连接同一张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21641248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com