[英]how to add two columns of two different rows have one same field in MySQL?
I have a MySQL database: results: 我有一个MySQL数据库:结果:
ID | B_ID | SUM
------------ |-------------|---------
1 | 400 | 10
2 | 500 | 20
3 | 500 | 30
4 | 400 | 40
But i want this: 但是我想要这个:
ID | B_ID | SUM
-------------|-------------|---------
1 | 400 | 50
2 | 500 | 50
Assuming that results
is an actual table, you can query it as follows: 假设
results
是一个实际表,则可以按以下方式查询它:
SELECT MIN(ID),
B_ID,
SUM(SUM)
FROM results
GROUP BY B_ID
If by "results" you mean that results
is the output from another query, then, without knowing what your original table looks like, you could subquery as follows: 如果用“结果”表示
results
是另一个查询的输出,那么在不知道原始表是什么样的情况下,可以按如下所示进行子查询:
SELECT MIN(t.ID),
t.B_ID,
SUM(t.SUM)
FROM
(
-- your original query goes here
) t
GROUP BY t.B_ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.