【问题标题】:How can I add up multiple columns in PHP and SQL with the same id?如何在 PHP 和 SQL 中添加多个具有相同 ID 的列?
【发布时间】:2014-11-27 17:01:49
【问题描述】:

我在一个 MySQL 表中有两列,一列称为“Total Experience”,一列称为“id”

有没有办法将 Total Experience 的值相加,并将其放入具有相同 id 的所有用户的变量中?

示例:有 3 个用户:Steve、Jack 和 Sam

Steve has the id of 1 and Total Experience of 500
Jack has the id of 1 and Total Experience of 400
Sam has the id of 2 and Total Experience of 700

有没有办法在 SQL 查询中选择和加起来 Steve 和 Jack 的 Total Experience,因为他们有相同的 id?

【问题讨论】:

  • 为什么标题说要加多列,但只加一列?
  • ^ 我也注意到了这一点。应该加起来多行。

标签: php mysql sql sum add


【解决方案1】:

使用GROUP BY 按列聚合行。

SELECT id, SUM(`Total Experience`) AS experience
FROM YourTable
GROUP BY id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2022-07-22
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多