我试图写一个子查询到左连接表,但我有错误,需要帮助找出我的错误,谢谢。
select IT.*,
(select firstname,lastname,sum(cost) as 2016_cost,agencyname
from sheet1
group by firstname,lastname,agencyname as aa)
from IT
left join aa on IT.lastname=aa.lastname,IT.firstname=aa.lastname
答案 0 :(得分:1)
看起来你想要这个:
SELECT IT.*, aa.firstname, aa.lastname, aa.2016_cost, aa.agencyname
FROM IT
LEFT JOIN (SELECT firstname, lastname, sum(cost) as 2016_cost, agencyname
FROM sheet1
GROUP BY firstname, lastname, agencyname
) as aa on IT.lastname = aa.lastname
and IT.firstname = aa.lastname
代码中的子查询位于SELECT
,您可以在其中放置列级子查询。您似乎想要一个表子查询或派生表,它应该放在您的FROM/JOIN
列表中。