我有很多板球碗的数据库。架构是:
Player(PlayerID, Initials, Surname)
- Stores all the players
Bowl(BowlID, Striker, Non-Striker, Bowler, Runs)
- Stores info for every ball
Team(TeamID, name)
- Stores all the teams
SquadPlayer(TeamID, PlayerID, MatchID)
- Stores a record of who was playing at each match and for which team
我试图找出英格兰队中每位球员的六分球数。(我使用的是 openoffice 风格的 HSQL)
SELECT "Player"."Initials", "Player"."Surname", COUNT ("Bowl"."Striker") AS "No. Sixes"
FROM "Bowl", "Player", "Player" AS "Bowler"
WHERE "Bowl"."Striker" = "Player"."PlayerID"
AND "Bowl"."Bowler" = "Bowler"."PlayerID"
AND "Bowl"."Striker" IN (
SELECT DISTINCT "Player"."PlayerID"
FROM "Player", "Team", "SquadPlayer"
WHERE "Player"."PlayerID" = "SquadPlayer"."PlayerID"
AND "Team"."TeamID" = "SquadPlayer"."TeamID"
AND "Team"."Name" = 'England' )
AND "Bowl"."Runs" = '6'
GROUP BY "Bowl"."Striker"
这是我试图运行的 SQL,但它不起作用。如果我省略了 SELECT 部分的前两列(Player.Initials 和 Player.Surname),它可以正常工作,但它不是很有用,因为我无法计算出 WHO 的所有六分之一。
我通过java运行它,我得到了异常:
java.sql.SQLException: Not in aggregate function or group by clause
那么为什么这不起作用呢?以及如何在旁边显示名称?