SQL聚合查询错误
我有3个这样的表
player(id,name,age,teamid)
team(id,name,sponsor,totalplayer,totalchampion,boss,joindate)
playerdetail(id,playerid,position,number,allstar,joindate)
我想选择团队信息包括姓名,赞助商,总球员,总冠军,老板, 球员的平均年龄,全明星球员的数量
我编写了如下t-sql
SELECT T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE,
AVG(P.AGE) AS AverageAge,COUNT(D.ALLSTAR) As AllStarPlayer
FROM Team T,Player P,PlayerDetail D
WHERE T.ID=P.TID AND P.ID=D.PID
,但它不起作用,错误消息是
'列'Team.Name'在选择列表中无效,因为它不是 包含在聚合函数或 GROUP BY 子句中。'
谁能帮助我? 提前谢谢!
I have 3 tables like this
player(id,name,age,teamid)
team(id,name,sponsor,totalplayer,totalchampion,boss,joindate)
playerdetail(id,playerid,position,number,allstar,joindate)
I want to select teaminfo include name,sponsor,totalplayer,totalchampion,boss,
the average age of the players, the number of the allstar players
I write the t-sql as below
SELECT T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE,
AVG(P.AGE) AS AverageAge,COUNT(D.ALLSTAR) As AllStarPlayer
FROM Team T,Player P,PlayerDetail D
WHERE T.ID=P.TID AND P.ID=D.PID
but it doesn't work, the error message is
'Column 'Team.Name' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.'
Who can help me?
Thx in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
大多数 RDBMS 中(MySQL 除外,它会为您猜测),列必须是聚合的(
COUNT
、AVG) 或在 GROUP BY 中
另外,您应该使用显式 JOIN。
这更清晰、更清晰、更难以修改你的代码
Add
In most RDBMS (except MySQL which will guess for you), a column must be either aggregated (
COUNT
,AVG
) or in the GROUP BYAlso, you should use explicit JOINs.
This is clearer, less ambiguous and more difficult to bollix your code
鉴于您需要每个团队的这些数据,并且 team.ID 唯一标识团队,我建议如下:
Given that you want this data per team, and team.ID uniquely identifies team, I suggest the following:
使用:
Use: