简体   繁体   English

MYSQL SUM和INNER JOIN

[英]MYSQL SUM and INNER JOIN

I want to have the name in the first colomn but i get an error. 我想在第一个栏目中输入名称,但出现错误。 When i Let P.name out, the query works but I can't see the player name. 当我让P.name发出时,查询有效,但我看不到播放器名称。

Right now I have the following query: 现在我有以下查询:

SELECT P.Name, P.Playernr, SUM (F.Amount)
FROM FINES F
INNER JOIN PLAYERS P
ON F.Playernr = P.Playernr
GROUP BY  P.Playernr

Thanks in advance for help 预先感谢您的帮助

Onno ONNO

You should add it to the GROUP BY clause. 您应该将其添加到GROUP BY子句。

SELECT P.Name, P.Playernr, SUM (F.Amount)
FROM FINES F
INNER JOIN PLAYERS P
ON F.Playernr = P.Playernr
GROUP BY  P.Name,P.Playernr

Try this: 尝试这个:

SELECT MAX(P.Name) as Name, P.Playernr, SUM (F.Amount)
FROM FINES F
INNER JOIN PLAYERS P
ON F.Playernr = P.Playernr
GROUP BY  P.Playernr

or add P.Name to the Group BY 或将P.Name添加到Group BY

SELECT P.Name, P.Playernr, SUM (F.Amount)
FROM FINES F
INNER JOIN PLAYERS P
ON F.Playernr = P.Playernr
GROUP BY  P.Name,P.Playernr

When GROUP BY used, only group by column and aggregation function can be projected. 当使用GROUP BY ,只能投影分组和汇总功能。 Remove P.Name in projection column or add P.Name in GROUP BY 在投影列中删除P.Name或在GROUP BY中添加P.Name

SELECT P.Name, P.Playernr, SUM (F.Amount)
FROM FINES F
INNER JOIN PLAYERS P
ON F.Playernr = P.Playernr
GROUP BY  P.Playernr, P.Name

In other RDBMS, your query produces error. 在其他RDBMS中,查询会产生错误。

think about below data: 考虑以下数据:

P.Playernr   P.Name
A         B
A         C
A         D

then, P.Name is not in GROUP BY, which value is produced in SELECT P.Playernr. 然后,P.Name不在GROUP BY中,该值在SELECT P.Playernr中产生。 so, you must use aggregation function or add P.Name in GROUP BY 因此,您必须使用聚合函数或在GROUP BY中添加P.Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM