我试图从租户表中获取最大租金将其加入到人员表中,该表将按姓氏名称对最大租金进行排序(数据是为了任务的目的而随机生成的。)下面的错误与此有关?
每个派生表都必须有自己的别名
这些是表格: Tenant Table People table
我使用以下代码:
SELECT PersonID, surname, tenantID, rent
FROM Tenants
INNER JOIN People
ON tenantID = People.ID
INNER JOIN
(
SELECT tenantID, MAX(rent) Max_Rent
FROM Tenants
GROUP BY tenantID
) ON Tenants.tenantID = People.tenantID AND
Tenants.rent = Max_rent)
答案 0 :(得分:-1)
您应该为每个子查询下的每个表添加一个别名,如下所示:
SELECT PersonID, surname, tenantID, rent
FROM Tenants T1
INNER JOIN People P1
ON T1.tenantID = P1.ID
INNER JOIN
(
SELECT T2.tenantID, MAX(T2.rent) Max_Rent
FROM Tenants T2
GROUP BY T2.tenantID
) ON T1.tenantID = P1.tenantID AND
T1.rent = Max_rent)