Raza Saleem
Raza Saleem

Reputation: 185

how to add two columns of two different rows have one same field in MySQL?

I have a MySQL database: results:

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

Upvotes: 2

Views: 29

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521684

Assuming that results is an actual table, you can query it as follows:

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:

SELECT MIN(t.ID),
       t.B_ID,
       SUM(t.SUM)
FROM
(
    -- your original query goes here
) t
GROUP BY t.B_ID

SQL Fiddle

Upvotes: 3

Related Questions