如何获取MySQL表中的值的总和

时间:2015-12-10 09:52:50

标签: mysql

让我们说一下这些记录的mysql表properties

---------------------------------
    id        value
---------------------------------
    1           1
    1             1
    2             1
    2             1
    1             1
---------------------------------

现在,我想查询输出:

----------------------------
    id           value
----------------------------
     1            3
     2            2
----------------------------

4 个答案:

答案 0 :(得分:1)

试试这个

select id,sum(value) as value FROM table group by id

答案 1 :(得分:0)

您需要使用GROUP BY参数。

SELECT id, SUM(value) as value FROM tablename GROUP BY id

答案 2 :(得分:0)

使用此代码

SELECT id, sum(value) AS value FROM `tablename ` Group by id;

答案 3 :(得分:0)

这就是你想要的:

select id, sum(value) as value from properties group by id