簡體   English   中英

SQL JOIN tables and and sum columns with the same PK

[英]SQL JOIN tables and and sum columns with the same PK

這是我的表:

球員

名稱 DoB 排行
1999 年 1 月 1 日 1個
湯姆 1999 年 2 月 1 日 1個
山姆 1999 年 3 月 1 日 1個
山姆 1999 年 3 月 1 日 2個

排行

排行 積分
1個 100
2個 50

我想加入表格並添加每個玩家的總分然后是 output 這個表格。

Output

名稱 DoB 積分
1999 年 1 月 1 日 100
湯姆 1999 年 2 月 1 日 100
山姆 1999 年 3 月 1 日 150

我目前正在使用這個聲明:

SELECT Matches.Name, 
       Matches.DoB, 
       Matches.Rankin, 
       SUM(Ranking.Points)
FROM Matches, Ranking
WHERE Matches.Ranking = Ranking.Ranking
GROUP BY Matches.Name

但是,當我運行代碼時出現此錯誤:

SELECT Matches.Name, Matches.Ranking, SUM(Ranking.Points)
FROM Matches, Ranking WHERE Matches.Ranking = Ranking.Ranking GROUP BY
Matches.Name LIMIT 0, 1000  Error Code: 1055. Expression #2 of SELECT
list is not in GROUP BY clause and contains nonaggregated column
'ad.Matches.Ranking' which is not functionally dependent on columns in
GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by 0.00044 sec

SQL 的新手,因此我們將不勝感激:)

您必須將 Select 中的所有字段添加到組中

SELECT Matches.Name, 
       Matches.DoB, 
       Matches.Rankin, 
       SUM(Ranking.Points)
FROM Matches, Ranking
WHERE Matches.Ranking = Ranking.Ranking
GROUP BY Matches.Name, Matches.DoB, Matches.Rankin

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM