我有一张表,我在下面提到过,我需要得到一个总结果,避免重复,任何人都可以提供任何帮助或建议如何获得下面提到的结果,thankyu
ID name Total Used
24 John 5 2
24 John 10 6
27 Peter 20 0
27 Peter 20 5
结果应该是这样的
ID name Total Used
24 John 15 8
27 Peter 40 5
答案 0 :(得分:9)
看起来您只需要在两列上使用SUM()
。还要在ID和名称
GROUP BY
SELECT id, name, sum(total) All_total, sum(used) All_used
FROM yourtable
GROUP BY id, name
GROUP BY
字段必须包含您选择的不在聚合函数中的任何其他列,因此对于该示例,您将在{{1}中包含id
和name
}}
编辑#1您的查询将是:
GROUP BY
答案 1 :(得分:1)
select sum(Total), sum(Used), ID, name
from table
group by ID, name;
答案 2 :(得分:0)
select ID, name, sum( Total,), sum( Used)
from table
group by Id,name;
答案 3 :(得分:0)
试试这个:
select id,name, sum(Total),sum(used)
from tab
group by id,name
答案 4 :(得分:0)
SELECT id, name, sum(total) total, sum(used) used from table GROUP BY id, name
答案 5 :(得分:0)
此问题的MySql语法:
CREATE TABLE DUPLICATE (ID,NAME, TOTAL, USED);
INSERT INTO DUPLICATE VALUES(24,'John',5,2);
INSERT INTO DUPLICATE VALUES(24,'John',10,6);
INSERT INTO DUPLICATE VALUES(27,'Peter',20,0);
INSERT INTO DUPLICATE VALUES(27,'Peter',20,5);
SELECT ID, NAME, SUM(TOTAL), SUM(USED) FROM DUPLICATE GROUP BY ID, NAME;