我目前正在尝试运行这段代码:
SELECT DISTINCT P.PlayerID
FROM Players as P, AllStar as A
WHERE P.PlayerID = A.PlayerID
GROUP BY P.PlayerID
HAVING COUNT(a.GamesPlayed) >= 8 // each GamesPlayed is always = 1
EXCEPT
SELECT DISTINCT P.PlayerID
FROM Players as P, HallOfFame as HOF
WHERE P.PlayerID = HOF.hofID OR P.Year BETWEEN 2010 AND 2017;
要找到所有参加过至少8场AllStar比赛的篮球运动员,不在名人堂,而且他们的最后几年都是在2010年之前。不幸的是,我目前的代码块并没有给我正确的权利。结果,但在我看来,我的逻辑检查在这里。有人可以帮我识别我在这里做错了吗?
非常感谢!
答案 0 :(得分:1)
WHERE
子句中的EXCEPT
子句看起来不对:
WHERE P.PlayerID = HOF.hofID OR P.Year BETWEEN 2010 AND 2017;
我想你想要一个LEFT JOIN
:
EXCEPT
SELECT DISTINCT P.PlayerID
FROM Players as P
LEFT JOIN HallOfFame as HOF
ON P.PlayerID = HOF.hofID
WHERE HOF.hofID IS NOT NULL OR P.Year BETWEEN 2010 AND 2017;
答案 1 :(得分:1)
我不会想到使用EXCEPT
解决这个问题。对我来说,这是一个带有WHERE
过滤器的聚合查询:
SELECT P.PlayerID
FROM Players P JOIN
AllStar A
ON P.PlayerID = A.PlayerID
WHERE NOT EXISTS (SELECT 1 FROM HallOfFame HOF WHERE P.PlayerID = HOF.hofID)
GROUP BY P.PlayerID
HAVING COUNT(a.GamesPlayed) >= 8 AND
MAX(P.Year) < 2010;