MySQL查询返回已连接列的总和

时间:2015-01-18 18:32:43

标签: mysql

当我运行以下查询时,它会返回9条记录。

Select
 snpc_stats.gamedetail.Finish As Rank,
 snpc_stats.gamedetail.Player As Player,
(snpc_stats.gamedetail.Points + snpc_stats.gamedetail.Hits) As Points,
snpc_stats.gamedetail.RoundOut As `Round Out`,
snpc_stats.gamedetail.Hits As Hits,
snpc_stats.gamedetail.Points As `Game Points`,
snpc_stats.gamedetail.Hitman As Hitman,
snpc_stats.gamedetail.TimeOut As `Time Out`,
snpc_stats.games.BuyIn As `Buy In`,
snpc_stats.gamedetail.ChampFund As `Champ Fund`,
snpc_stats.gamedetail.Winnings As Winnings,
(replace(replace(snpc_stats.gamedetail.Winnings, '$', ''), ',', '') -
 replace(replace(snpc_stats.games.BuyIn, '$', ''), ',', '') -
 replace(replace(snpc_stats.gamedetail.ChampFund, '$', ''), ',', '')) As Net,
snpc_stats.games.Round,
snpc_stats.games.GameDate,
snpc_stats.games.Location

From
snpc_stats.gamedetail Inner Join
snpc_stats.games On snpc_stats.games.GameID = snpc_stats.gamedetail.GamesID

Where
snpc_stats.gamedetail.GamesID = 113

当我尝试将以下内容添加到SELECT的末尾时,它只返回1条记录

, SUM(replace(replace(snpc_stats.games.BuyIn, '$', ''), ',', '')) As Purse

Winnings,Buyin和ChampFund是VARCHAR dataype。我知道他们应该是INT,但其他人最初做了db ...

如果有人可以帮我弄清楚如何在总计“买入”栏目的同时归还所有9条记录,我将不胜感激!它很接近。

1 个答案:

答案 0 :(得分:0)

SUM句子是一个聚合函数,这意味着它会添加所有返回的行,这就是你只得到一行的原因:

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

如果您想通过某些属性添加,则必须在字段定义中添加SUM(),并在查询末尾添加GROUP BY子句,并指定要分组的字段。类似的东西:

GROUP BY player [, more fields you wanted to group by]