【问题标题】:How can I use SUM() to sum my result array?如何使用 SUM() 对结果数组求和?
【发布时间】:2011-04-25 05:08:42
【问题描述】:

我当前将行添加在一起的方法是这样的:

$totalxp = $row['Attackxp'] + $row['Defencexp'] + $row['Strengthxp'] + $row['Hitpointsxp'] + $row['Rangedxp'] + $row['Prayerxp'] + $row['Magicxp'] + $row['Cookingxp'] + $row['Woodcuttingxp'] + $row['Fletchingxp'] + $row['Fishingxp'] + $row['Firemakingxp'] + $row['Craftingxp'] + $row['Smithingxp'] + $row['Miningxp'] + $row['Herblorexp'] + $row['Agilityxp'] + $row['Thievingxp'] + $row['Slayerxp'] + $row['Farmingxp'] + $row['Runecraftxp'] + $row['Constructionxp'];

但后来我看到了 SUM() 并尝试了这个:

SELECT SUM(xp) FROM skills WHERE playerName='Undercover' 

它可以工作,但我需要 xp 的所有值,所以我尝试添加 %xp,但它不起作用。

如何使用 Sum() 函数将所有行加起来而不是使 PHP 紧张?

【问题讨论】:

  • 我们可以看看你的表结构吗?
  • 值是全部在不同列的同一行,还是在同一列的多行? SUM 适用于同一列/表达式中多行的聚合。

标签: sql mysql aggregate-functions


【解决方案1】:

聚合函数(即:SUM、MIN、MAX、COUNT 等)不能跨列工作——它们根据分组 (GROUP BY) 和过滤 (@987654322) 处理特定列的值@ 和/或 WHERE 子句)。

要跨列累加值,您需要像对普通数学方程一样进行累加:

SELECT Attackxp + Defencexp + Strengthxp + Hitpointsxp + Rangedxp + Prayerxp + Magicxp + Cookingxp+ Woodcuttingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftxp + Constructionxp AS total_xp
  FROM skills 
 WHERE playerName = 'Undercover' 

如果您有多个记录与玩家姓名相关联,那么您可以使用聚合函数:

SELECT SUM(Attackxp + Defencexp + Strengthxp + Hitpointsxp + Rangedxp + Prayerxp + Magicxp + Cookingxp+ Woodcuttingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftxp + Constructionxp) AS total_xp
  FROM skills 
 WHERE playerName = 'Undercover'

【讨论】:

  • +1 你的声望现在四舍五入到 56k。 -突然的怀旧之情-
  • 我不明白“不能跨列工作”,并且基于分组它们适用于特定列的说法并不完全正确。
  • @Vash:我不清楚你不明白什么。除了 COUNT 接受 * 之外,我想不出任何可以接受多个列作为参数的聚合。
  • @OMG 现在我明白你的想法了,函数不允许使用多个参数。
  • @Vash:很抱歉造成误会。
【解决方案2】:

如果每个玩家都是一个实体(行),则取决于表数据,然后可以添加列:

SELECT Attackxp  + Defencexp + Strengthxp + Hitpointsxp +Rangedxp + Prayerxp + Magicxp + Cookingxp + Woodcuttingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftxp + Constructionxp 
As totalSkills FROM skills WHERE playerName = 'Undercover'

但是每个玩家是否有更多的行然后你需要对行求和

SELECT SUM(Attackxp  + Defencexp + Strengthxp + Hitpointsxp +Rangedxp + Prayerxp + Magicxp + Cookingxp + Woodcuttingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftxp + Constructionxp) 
As totalSkills FROM skills WHERE playerName = 'Undercover'

【讨论】:

    【解决方案3】:
    SELECT SUM(`Attackxp`) + SUM(`Defencexp`) + ... AS `total_sum`
      FROM skills
     WHERE playerName='Undercover' 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多