数据库计数&分组错误

时间:2013-03-24 21:08:41

标签: mysql sql database count group-by

我对SQL很新,我正在努力提高自己。

我有一个有

的数据库

表:玩家,团队,游戏和胜利

玩家:pid,pname,年龄,国家

比赛:pid,season,tid,value(pid - > pid in Players,tid - > tid in teams)

团队:tid,tname,tcolor,tbudget

胜利:wtid,ltid,season,wscore,lscore(wtid,ltid - > tid in teams)


问题是Find the name of the players whose played in atleast 2 dif. teams with same color

我做的是

SELECT DISTINCT P.pname 
FROM Players P
    ,Teams T1 
GROUP BY T1.tcolor
HAVING 1 < (
   SELECT COUNT (10)
   FROM Teams T2
   WHERE T1.tcolor=T2.tcolor)

当我尝试查询时,我收到错误;

Error Code: 1630
FUNCTION PRALATEST.COUNT does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual

我做错了哪一部分?

2 个答案:

答案 0 :(得分:3)

试试这个:

select pname
from Players
join Plays on Plays.pid = Players.pid
join Teams on Teams.tid = Plays.tid
group by pname, tcolor
having count(Teams.tname) > 1

条件count(Teams.tname) > 1在having子句而不是where子句中,因为它需要在执行group by之后对结果进行操作。

答案 1 :(得分:1)

夫妻俩。您的错误消息是因为您在COUNT函数中放了一个数字常量。你应该使用星号。

此外,您尚未为PlayersTeams表指定加入条件。因此,您正在进行产品加入(可能不是您想要的)。我猜你需要加入你的Plays表。

你应该改变你的编码习惯,使用&#34; explicit&#34;加入语法以避免将来出现这样的错误。