Mysql 如何使用Count计算每个玩家的行数

Mysql 如何使用Count计算每个玩家的行数,mysql,database,Mysql,Database,我正在使用拉曼棒球数据库,试图找出如何计算一名球员为多少支不同的球队效力,这样我就可以用它作为子查询来查找只为洛杉矶队和布鲁克林道奇队效力的球员 这就是我目前所拥有的,我知道这是不对的,因为它给了我错误的结果 SELECT M.masterID, M.nameFirst, M.nameLast, A.teamID, count( DISTINCT A.teamID) as numberOFteamsPlayedfor from master as M, appearances as A whe

我正在使用拉曼棒球数据库,试图找出如何计算一名球员为多少支不同的球队效力,这样我就可以用它作为子查询来查找只为洛杉矶队和布鲁克林道奇队效力的球员

这就是我目前所拥有的,我知道这是不对的,因为它给了我错误的结果

SELECT  M.masterID, M.nameFirst, M.nameLast, A.teamID, count( DISTINCT A.teamID) as numberOFteamsPlayedfor
from master as M, appearances as A
where M.masterID = A.masterID
group by A.teamID
这是我现在正在处理的两张表

这些是表格结构

CREATE TABLE "master" (
 "masterID" varchar(10) NOT NULL,
 "birthYear" int DEFAULT NULL,
  "birthMonth" int DEFAULT NULL,
  "birthDay" int DEFAULT NULL,
 "birthCountry" varchar(50) DEFAULT NULL,
 "birthState" varchar(2) DEFAULT NULL,
 "birthCity" varchar(50) DEFAULT NULL,
 "deathYear" int DEFAULT NULL,
 "deathMonth" int DEFAULT NULL,
"deathDay" int DEFAULT NULL,
"deathCountry" varchar(50) DEFAULT NULL,
"deathState" varchar(2) DEFAULT NULL,
"deathCity" varchar(50) DEFAULT NULL,
"nameFirst" varchar(50) DEFAULT NULL,
"nameLast" varchar(50) DEFAULT NULL,
 "nameGiven" varchar(255) DEFAULT NULL,
 "weight" int DEFAULT NULL,
 "height" float DEFAULT NULL,
  "bats" varchar(1) DEFAULT NULL,
  "throws" varchar(1) DEFAULT NULL,
 "debut" datetime  DEFAULT NULL,
"finalGame" datetime  DEFAULT NULL,
"retroID" varchar(9) DEFAULT NULL,
"bbrefID" varchar(9) DEFAULT NULL,
  PRIMARY KEY ("masterID")
);
 CREATE TABLE "appearances" (
 "yearID" int NOT NULL DEFAULT '0',
 "teamID" varchar(3) NOT NULL DEFAULT '',
 "lgID" varchar(2) DEFAULT NULL,
 "masterID" varchar(9) NOT NULL DEFAULT '',
 "G_all" int DEFAULT NULL,
 "GS" int DEFAULT NULL,
 "G_batting" int DEFAULT NULL,
 "G_defense" int DEFAULT NULL,
 "G_p" int DEFAULT NULL,
 "G_c" int DEFAULT NULL,
 "G_1b" int DEFAULT NULL,
 "G_2b" int DEFAULT NULL,
 "G_3b" int DEFAULT NULL,
 "G_ss" int DEFAULT NULL,
 "G_lf" int DEFAULT NULL,
 "G_cf" int DEFAULT NULL,
 "G_rf" int DEFAULT NULL,
 "G_of" int DEFAULT NULL,
 "G_dh" int DEFAULT NULL,
 "G_ph" int DEFAULT NULL,
 "G_pr" int DEFAULT NULL,
 PRIMARY KEY ("yearID","teamID","masterID")

))

“count(DISTINCT A.teamID)”和“groupby A.teamID”是多余的,因此您的计数将分组两次。将“count(DISTINCT A.teamID)”更改为“count(A.teamID)”,GROUP BY将为您解决此问题。

我已经尝试过,但结果不正确,我很确定Hank aaron没有为M.masterID的1950个不同的Teamsry GROUP效力。“我有点瞎了,看不到桌子的结构,……但这会让你看到每个球员,而不是每个球队。我也尝试过,但结果仍然是错误的,”汉克·艾伦说,他为23支不同的球队效力。我在我的问题中加入了表格结构,是“按年度和按团队”……因此,汉克·阿伦(Hank Aaron)每个赛季为一支球队效力23个赛季是有意义的,而且数量上的差异也是有意义的。如果你把它扔回去,但是把新的团队留在旁边,你应该在肥城。