1
我正在使用拉曼棒球數據庫http://seanlahman.com/files/database/readme2012.txt,並試圖弄清楚如何計算一個玩家玩過多少個不同的團隊,這樣我就可以使用它作爲一個子查詢找到誰發揮只爲洛杉磯和布魯克林道奇隊我如何使用Count來統計每個玩家有多少行
球員這是我到目前爲止,我知道這不好導致其給我的錯誤的結果
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")
);
我曾試過,但它給了我錯誤的結果,我很確定漢克亞倫沒有參加1950年不同的球隊 –
嘗試GROUP BY M.masterID。我有點瞎了,看不到桌子的結構,但是這會給你每個球員,而不是每個球隊。 – MichaelClark
我已經嘗試過,但它仍然給出錯誤的結果,說阿倫爲23個不同的球隊效力。我把表格結構添加到了我的問題 –