如何在具有相同ID的PHP和SQL中添加多个列?

时间:2014-11-27 17:01:50

标签: php mysql sql sum add

我在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?

1 个答案:

答案 0 :(得分:1)

使用GROUP BY按列聚合行。

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