显式连接到隐式连接sql

时间:2016-10-16 15:02:00

标签: mysql inner-join

我是SQL新手并且一直在尝试使用显式JOIN编写查询,因为它比隐式JOIN更好的性能

SELECT 
r.TicketNumber,
r.VendorNumber,
r.VendorName,
rd.FromCityCode,
rd.ToCityName,
et.TravelDate,
tc.TotalAmount,
tc.AmountInvoiced,
FROM Reservations as r,
ReservationDetails as rd,
Trips as t,
TripCosts as tc
WHERE r.ProductCode='1'
AND t.TravelDate < '2016-11-23 00:00:00.0'
AND t.TravelDate > '2016-10-23 00:00:00.0'
AND t.TripID = r.TripID
AND r.ReservationID=rd.ReservationID
AND t.TripID = tc.TripID;

当我把它变成INNER JOIN时

SELECT 
r.TicketNumber,
r.VendorNumber,
r.VendorName,
rd.FromCityCode,
rd.ToCityName,
t.TravelDate,
tb.TotalAmount,
tb.AmountInvoiced
FROM Reservations as r
  JOIN ReservationDetails as rd ON rd.ReservationID=r.ReservationID
  JOIN Trips as t ON TripID = r.TripID
  JOIN TripBalances as tb ON tb.TripID = t.TripID
WHERE r.ProductCode='1'
AND t.TravelDate < '2016-11-23 00:00:00.0'
AND t.TravelDate > '2016-10-23 00:00:00.0'

当我运行内连接查询时,我正在

Error: Ambiguous column name 'TripID'.
SQLState:  S0001
ErrorCode: 209

我做错了什么?

1 个答案:

答案 0 :(得分:1)

因为您在alias

中缺少TripID名称
JOIN Trips as t ON t.TripID = r.TripID
                 --^here

使用一个而不是另一个没有性能提升。显式连接比隐式连接更具可读性。两者都有相同的执行计划